版本比较
栈、队列和STL的故事
正文
增加 38 行删除 38 行
123456734 行未改动42434445464748495022 行未改动73747576777879808116 行未改动989910010110210310410510622 行未改动12913013113213313413513613727 行未改动16516616716816917017117217310 行未改动18418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821921 行未改动24124224324424524624724824920 行未改动2702712722732742752762772782792802812822838 行未改动29229329429529629729829930030130241 行未改动34434534634734834935035135235335435535635735835936036110 行未改动37237337437537637737837938014 行未改动39539639739839940040140240340440540640740840941041141241325 行未改动43944044144244344444544644711 行未改动4594604614624634644654664674684695 行未改动4754764774784794804814824836 行未改动49049149249349449549649749849950050150244 行未改动5475485495505515525535545557 行未改动563564565566567568569570571注:各位可以在练习题处先练习(请不要抄袭)## 栈**(一)用数组模拟栈**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;34 行未改动}return 0;}```**(二)STL栈(操作同上)**```cpp#include <bits/stdc++.h>using namespace std;stack <int> a;22 行未改动}return 0;}```**(三)用栈把十进制数转换为八进制数(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;16 行未改动}return 0;}```**(四)[表达式括号匹配](https://www.luogu.com.cn/problem/P1739)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;string s;22 行未改动cout<<(k?"YES":"NO");return 0;}```**(五)[表达式求值](https://www.luogu.com.cn/problem/P1981)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int M=10000;27 行未改动cout<<n;return 0;}```**(六)[铁轨 Rails](https://www.luogu.com.cn/problem/UVA514)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e3 + 50;10 行未改动{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队列**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;21 行未改动while (!q.empty()) q.pop();return 0;}```**(二)[新生舞会](https://www.acwing.com/problem/content/6037/)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;queue <int> q1,q2;20 行未改动}return 0;}```或如下代码```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5 + 20;int q1[N],q2[N],st1,st2,ed1,ed2;int main(){8 行未改动q1[++ed1]=q1[st1++];q2[++ed2]=q2[st2++];}return 0;}```**(三)[团体队列](https://www.luogu.com.cn/problem/UVA540)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;queue<int> q[1010];41 行未改动for(int i = 0;i<=t;i++) while(!q[i].empty()) q[i].pop();cout<<"\n";}return 0;}```**(四)[蚯蚓](https://www.luogu.com.cn/problem/P2827)(练习题)**```cpp#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(){10 行未改动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();14 行未改动{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集合**```cpp#include <bits/stdc++.h>using namespace std;multiset <int> s;25 行未改动{int x;cin>>x;set <int>::iterator it=s.find(x);s.erase(it);}else if (op==4){int x;11 行未改动cout<<*it<<" ";it++;}return 0;}```**(二)STL映射(map)**```cpp#include <bits/stdc++.h>using namespace std;map <string,int> mp;5 行未改动cin>>n;for (int i = 1;i<=n;i++){string s;int id;cin>>s>>id;mp[s]=i;}int m;6 行未改动}return 0;}```**(三)[集合栈计算机](https://www.luogu.com.cn/problem/UVA12096)(练习题)**```cpp#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;44 行未改动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));7 行未改动int T;cin>>T;while (T--) solve();return 0;}```题外话:本人蒟蒻,如有不正确或可优化的地方,欢迎各位巨佬指教QAQ各位支持一下[高精度的故事](https://www.luogu.com.cn/article/m43odcr4)、[排序的故事](https://www.luogu.com.cn/article/21d7l7bb)和[背包DP的故事](https://www.luogu.com.cn/article/g4log0d8)这三篇文章呗123456734 行未改动42434445464748495022 行未改动73747576777879808116 行未改动989910010110210310410510622 行未改动12913013113213313413513613727 行未改动16516616716816917017117217310 行未改动18418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821921 行未改动24124224324424524624724824920 行未改动2702712722732742752762772782792802812822838 行未改动29229329429529629729829930030130241 行未改动34434534634734834935035135235335435535635735835936036110 行未改动37237337437537637737837938014 行未改动39539639739839940040140240340440540640740840941041141241325 行未改动43944044144244344444544644711 行未改动4594604614624634644654664674684695 行未改动4754764774784794804814824836 行未改动49049149249349449549649749849950050150244 行未改动5475485495505515525535545557 行未改动563564565566567568569570571注:各位可以在练习题处先练习(请不要抄袭)## 栈**(一)用数组模拟栈**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;34 行未改动}return 0;}```**(二)STL栈(操作同上)**```cpp#include <bits/stdc++.h>using namespace std;stack <int> a;22 行未改动}return 0;}```**(三)用栈把十进制数转换为八进制数(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;16 行未改动}return 0;}```**(四)[表达式括号匹配](https://www.luogu.com.cn/problem/P1739)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;string s;22 行未改动cout<<(k?"YES":"NO");return 0;}```**(五)[表达式求值](https://www.luogu.com.cn/problem/P1981)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int M=10000;27 行未改动cout<<n;return 0;}```**(六)[铁轨 Rails](https://www.luogu.com.cn/problem/UVA514)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e3 + 50;10 行未改动{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队列**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;21 行未改动while (!q.empty()) q.pop();return 0;}```**(二)[新生舞会](https://www.acwing.com/problem/content/6037/)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;queue <int> q1,q2;20 行未改动}return 0;}```或如下代码```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5 + 20;int q1[N],q2[N],st1,st2,ed1,ed2;int main(){8 行未改动q1[++ed1]=q1[st1++];q2[++ed2]=q2[st2++];}return 0;}```**(三)[团体队列](https://www.luogu.com.cn/problem/UVA540)(练习题)**```cpp#include <bits/stdc++.h>using namespace std;queue<int> q[1010];41 行未改动for(int i = 0;i<=t;i++) while(!q[i].empty()) q[i].pop();cout<<"\n";}return 0;}```**(四)[蚯蚓](https://www.luogu.com.cn/problem/P2827)(练习题)**```cpp#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(){10 行未改动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();14 行未改动{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集合**```cpp#include <bits/stdc++.h>using namespace std;multiset <int> s;25 行未改动{int x;cin>>x;set <int>::iterator it=s.find(x);s.erase(it);}else if (op==4){int x;11 行未改动cout<<*it<<" ";it++;}return 0;}```**(二)STL映射(map)**```cpp#include <bits/stdc++.h>using namespace std;map <string,int> mp;5 行未改动cin>>n;for (int i = 1;i<=n;i++){string s;int id;cin>>s>>id;mp[s]=i;}int m;6 行未改动}return 0;}```**(三)[集合栈计算机](https://www.luogu.com.cn/problem/UVA12096)(练习题)**```cpp#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;44 行未改动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));7 行未改动int T;cin>>T;while (T--) solve();return 0;}```题外话:本人蒟蒻,如有不正确或可优化的地方,欢迎各位巨佬指教QAQ各位支持一下[高精度的故事](https://www.luogu.com.cn/article/m43odcr4)、[排序的故事](https://www.luogu.com.cn/article/21d7l7bb)和[背包DP的故事](https://www.luogu.com.cn/article/g4log0d8)这三篇文章呗