跳到正文

个人记录

栈、队列和STL的故事

zhanghanze123约 16 分钟
原文
文章信息与版本

文章信息

发布
2025年7月9日
收录
2025年12月24日
更新
2026年11月15日
篇幅
约 16 分钟

版本

  1. v2当前
  2. v1

注:各位可以在练习题处先练习(请不要抄袭)

栈

(一)用数组模拟栈

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
int n,top,s[N];


void push(int x)
{
	s[++top]=x;
}
int pop()
{
	return s[top--];
}
bool empty()
{
	return top==0;
}
int size()
{
	return top;
}
int main()
{
	cin>>n;
	while (n--)
	{
		int op,x;
		cin>>op;
		if (op==0)
		{
			cin>>x;
			push(x);
		}
		else if (op==1) cout<<pop()<<"\n";
		else if (op==2) cout<<(empty()?"Yes\n":"No\n");
		else if (op==3) cout<<size()<<"\n";
	}
	return 0;
}

(二)STL栈(操作同上)

C++
#include <bits/stdc++.h>
using namespace std;
stack <int> a;
int n;


int main()
{
	cin>>n;
	while (n--)
	{
		int op,x;
		cin>>op;
		if (op==0)
		{
			cin>>x;
			a.push(x);
		}
		else if (op==1&&!a.empty())
		{
			cout<<a.top()<<"\n";
			a.pop();
		}
		else if (op==2) cout<<(a.empty()?"Yes\n":"No\n");
		else if (op==3) cout<<a.size()<<"\n";
	}
	return 0;
}

(三)用栈把十进制数转换为八进制数(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
stack <int> s;
int x;


int main()
{
	cin>>x;
	while (x)
	{
		s.push(x%8);
		x/=8;
	}
	while (!s.empty())
	{
		cout<<s.top();
		s.pop();
	}
	return 0;
}

(四)表达式括号匹配(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
string s;
int len,top;
bool k=1;


int main()
{
	cin>>s;
	len=s.size();
	for (int i = 0;i<len;i++)
	{
		if (s[i]=='(') top++;
		else if (s[i]==')')
		{
			if (top==0)
			{
				k=0;
				break;
			}
			top--;
		}
	}
	if (top) k=0;
	cout<<(k?"YES":"NO");
	return 0;
}

(五)表达式求值(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
const int M=10000;
stack <int> a;
int n,m;
char c;


int main()
{
	cin>>n;
	n%=M;
	a.push(n);
	while (cin>>c>>m)
	{
		if (c=='*')
		{
			n=a.top();
			a.pop();
			a.push(n*m%M);
		}
		else a.push(m);
	}
	n=0;
	while (!a.empty())
	{
		n+=a.top();
		n%=M;
		a.pop();
	}
	cout<<n;
	return 0;
}

(六)铁轨 Rails(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e3 + 50;
int n,t[N];


int main()
{
	while (1)
	{
		cin>>n;
		if (n==0) break;
		while (1)
		{
			cin>>t[1];
			if (t[1]==0) break;
			stack <int> s;
			while (!s.empty()) s.pop();
			int A=1,B=1;
			for (int i = 2;i<=n;i++) cin>>t[i];
			int ok=1;
			while (B<=n)
			{
				if (A==t[B])
				{
					A++;
					B++;
				}
				else if (!s.empty()&&s.top()==t[B])
				{
					s.pop();
					B++;
				}
				else if (A<=n) s.push(A++);
				else ok = 0; break;
			}
			cout<<(ok?"Yes":"No");
		}
		cout<<"\n";
	}
	return 0;
 }

队列

(一)STL队列

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
int n,op,x;
queue <int> q;


int main()
{
	cin>>n;
	while (n--)
	{
		cin>>op;
		if (op==0)
		{
			cin>>x;
			q.push(x);
		}
		else
		{
			cout<<"out:"<<q.front()<<"\n";
			q.pop();
		}
	}
	while (!q.empty()) q.pop();
	return 0;
}

(二)新生舞会(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
queue <int> q1,q2;
int n,m,k,n1=1,m1=1;


int main()
{
	cin>>n>>m>>k;
	for (int i = 1;i<=k;i++)
	{
		q1.push(n1);
		q2.push(m1);
		n1++;
		m1++;
		if (n1>n) n1=1;
		if (m1>m) m1=1;
	}
	for (int i = 1;i<=k;i++)
	{
		cout<<q1.front()<<" "<<q2.front()<<"\n";
		q1.pop();
		q2.pop();
	}
	return 0;
}

或如下代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5 + 20;
int q1[N],q2[N],st1,st2,ed1,ed2;


int main()
{
	st1=st2=1;
	int n,m,k;
	cin>>n>>m>>k;
	for (int i = 1;i<=n;i++) q1[++ed1]=i;
	for (int i = 1;i<=m;i++) q2[++ed2]=i;
	while (k --)
	{
		cout<<q1[st1]<<" "<<q2[st2]<<"\n";
		q1[++ed1]=q1[st1++];
		q2[++ed2]=q2[st2++];
	}
	return 0;
}

(三)团体队列(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
queue<int> q[1010];
int m[1000100],cnt;


int main()
{
	while (1)
	{
		int t;
		cin>>t;
		if(t==0) break;
		cout<<"Scenario #"<<++cnt<<"\n";
		for (int i = 1;i<=t;i++)
		{
			int p;
			cin>>p;
			for (int j = 1;j<=p;j++)
			{
				int x;
				cin>>x;
				m[x]=i;
			}
		}
		while(1)
		{
			string s;
			cin>>s;
			if (s[0]=='E')
			{
				int x;
				cin>>x;
				if(q[m[x]].empty()) q[0].push(m[x]);
				q[m[x]].push(x);
			}
			if (s[0]=='D')
			{
				cout<<q[q[0].front()].front()<<"\n";
				q[q[0].front()].pop();
				if(q[q[0].front()].empty()) q[0].pop();
			}
			if (s[0]=='S') break;
		}
		for(int i = 0;i<=t;i++) while(!q[i].empty()) q[i].pop();
		cout<<"\n";
	}
	return 0;
}

(四)蚯蚓(练习题)

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5 + 50;
int n,m,Q,u,v,t,P,a[N];
queue <int> q[3];


int main()
{
	cin>>n>>m>>Q>>u>>v>>t;
	for (int i = 1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for (int i = 1;i<=n;i++) q[0].push(a[i]);
	for (int i = 1;i<=m;i++)
	{
		int mx=0,my=-INT_MAX;
		for (int j = 0;j<=2;j++)
		{
			if (!q[j].empty()&&q[j].front()>my)
			{
				my=q[j].front();
				mx=j;
			}
		}
		my+=P;
		if (i%t==0) cout<<my<<" ";
		q[mx].pop();
		int x=1ll*my*u/v;
		int y=my-x;
		P+=Q;
		q[1].push(x-P);
		q[2].push(y-P);
	}
	cout<<"\n";
	for (int j = 1;;j++)
	{
		bool k=0;
		int mx,my=-INT_MAX;
		for (int i = 0;i<=2;i++)
		{
			if(!q[i].empty()&&q[i].front()>my)
			{
				my=q[i].front();
				mx=i;
				k=1;
			}
		}
		if (!k) break;
		if (j%t==0) cout<<my+P<<" ";
		q[mx].pop();
	}
	return 0;
}

STL

(一)STL集合

C++
#include <bits/stdc++.h>
using namespace std;
multiset <int> s;
int m;


int main()
{
	cin>>m;
	while (m--)
	{
		int op;
		cin>>op;
		if (op==1)
		{
			int x;
			cin>>x;
			s.insert(x);
		}
		else if (op==2)
		{
			int x;
			cin>>x;
			set <int>::iterator it=s.find(x);
			if(it==s.end()) cout<<"Not Found\n";
			else cout<<*it<<"\n";
		}
		else if (op==3)
		{
			int x;
			cin>>x;
			set <int>::iterator it=s.find(x);
			s.erase(it);
		}
		else if (op==4)
		{
			int x;
			cin>>x;
			set <int>::iterator it=s.lower_bound(x);
			cout<<*it<<"\n";
		}
	}
	cout<<"set size:"<<s.size()<<"\n";
	for (auto i : s) cout<<i<<" ";
	cout<<"\n";
	set <int>::iterator it=s.begin();
	while (it!=s.end())
	{
		cout<<*it<<" ";
		it++;
	}
	return 0;
}

(二)STL映射(map)

C++
#include <bits/stdc++.h>
using namespace std;
map <string,int> mp;


int main()
{
	int n;
	cin>>n;
	for (int i = 1;i<=n;i++)
	{
		string s;
		int id;
		cin>>s>>id;
		mp[s]=i;
	}
	int m;
	cin>>m;
	for (int i = 1;i<=m;i++)
	{
		string s;
		cin>>s;
		cout<<mp[s]<<"\n";
	}
	return 0;
}

(三)集合栈计算机(练习题)

C++
#include <bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
stack <int> s;
map <set <int>,int> IDset;
vector <set <int>> sets;


int ID(set <int> x)
{
	if (IDset[x]) return IDset[x];
	sets.push_back(x);
	return IDset[x]=sets.size() - 1;
}
set <int> set_union(set <int> x1,set <int> x2)
{
	set <int>::iterator x=x2.begin();
	while (x!=x2.end())
	{
		x1.insert(*x);
		x++;
	}
	return x1;
}
set <int> set_inter(set <int> x1,set <int> x2)
{
	set <int> x;
	x.clear();
	for (auto i : x1) if (x2.find(i)!=x2.end()) x.insert(i);
	return x;
}
void clear()
{
	while (!s.empty()) s.pop();
	IDset.clear();
	sets.clear();
}
void solve()
{
	clear();
	int n;
	cin>>n;
	while (n--)
	{
		string op;
		cin>>op;
		if (op[0]=='P') s.push(ID(set <int> ()));
		else if (op[0]=='D') s.push(s.top());
		else
		{
			set <int> x1=sets[s.top()];
			s.pop();
			set <int> x2=sets[s.top()];
			s.pop();
			set <int> x;
			if(op[0]=='U') set_union(ALL(x1),ALL(x2),INS(x));
			if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));
			if(op[0]=='A') x=x2,x.insert(ID(x1));
			s.push(ID(x));
		}
		cout<<sets[s.top()].size()<<"\n";
	}
	cout<<"***\n";
}
int main()
{
	int T;
	cin>>T;
	while (T--) solve();
	return 0;
}

题外话:本人蒟蒻,如有不正确或可优化的地方,欢迎各位巨佬指教QAQ

各位支持一下高精度的故事、排序的故事和背包DP的故事这三篇文章呗

原文