版本比较
栈、队列和STL的故事
正文
增加 38 行删除 38 行
123123456734 行未改动4243444546464748495022 行未改动7374757677777879808116 行未改动989910010110210210310410510622 行未改动12913013113213313313413513613727 行未改动16516616716816916917017117217310 行未改动18418518618718818818919019119219319419419519619719819919920020120220320420520620720820920921021121221221321421521421521621721821921 行未改动24124224324424524524624724824920 行未改动2702712722732742742752762772782792792802812822838 行未改动29229329429529629629729829829930030130241 行未改动34434534634734834834935035135135235335435535635735735835936036110 行未改动37237337437537637637737837938014 行未改动39539639739839939940040140240340440340440540640740840940840941041141241325 行未改动43944044144244344344444544644711 行未改动4594604614624634634644654654664674684695 行未改动4754764774784794794804814824836 行未改动49049149249349449449549649749849849950050150244 行未改动5475485495505515515525535545557 行未改动563564565566567567568569569570571注:各位可以在练习题处先练习(请不要抄袭) ## 栈 **(一)用数组模拟栈** 注:各位可以在练习题处先练习(请不要抄袭)## 栈**(一)用数组模拟栈**```cpp#include <bits/stdc++.h>using namespace std;const int N=1e5+50;34 行未改动 } return 0;}```**(二)STL栈(操作同上)** **(二)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)(练习题)** **(四)[表达式括号匹配](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)(练习题)** **(五)[表达式求值](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)(练习题)** **(六)[铁轨 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(); 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]) if (A==t[B]) { A++; B++; } else if (!s.empty()&&s.top()==t[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"; cout<<"\n"; } return 0; } }```## 队列 **(一)STL队列** ## 队列**(一)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/)(练习题)** **(二)[新生舞会](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 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)(练习题)** **(三)[团体队列](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)(练习题)** **(四)[蚯蚓](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]; 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(); } q[mx].pop(); } return 0;}```## STL **(一)STL集合** ## 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); s.erase(it); } else if (op==4) { int x;11 行未改动 cout<<*it<<" "; it++; } return 0;} }```**(二)STL映射(map)** **(二)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; int id; cin>>s>>id; mp[s]=i; } int m;6 行未改动 } return 0;}```**(三)[集合栈计算机](https://www.luogu.com.cn/problem/UVA12096)(练习题)** **(三)[集合栈计算机](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()) #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; 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 题外话:本人蒟蒻,如有不正确或可优化的地方,欢迎各位巨佬指教QAQ
各位支持一下[高精度的故事](https://www.luogu.com.cn/article/m43odcr4)、[排序的故事](https://www.luogu.com.cn/article/21d7l7bb)和[背包DP的故事](https://www.luogu.com.cn/article/g4log0d8)这三篇文章呗