ʕ·͡ˑ·ཻ ʕ•̫͡• ʔ•̫͡•ཻʕ•̫͡•ʔ•͓͡•ʔ

  STL(Standard Template Library)是C++的标准模板库熟练掌握它们在很多题目中能极大地简化编程,需要完全掌握。
  STL包括容器(container)、迭代器(iterator)、空间配置器(allocator)、配接器(adapter)、算法(algorithm)、仿函数(functor)6个部分。

  容器

  1.顺序式容器
vector :动态数组,从末尾能快速插入与删除,直接访问任何元素。

list :双链表,从任何地方快速插入和删除。

deque :双向队列,从前面或后面快速插入与删除,直接访问任何元素。

queue:队列,先进先出。

priority_queue:优先队列,最高优先级元素总是第一个出列。

stack:栈,后进先出。

  2.关联式容器
set:集合,快速查找,不允许重复值。

map:一对多映射,基于关键字快速查找,不允许重复值。

multiset:快速查找,允许重复值。

multimap:一对多映射,基于关键字快速查找,允许重复值。

  vector
hdu 4841 “圆桌问题”

[https://vjudge.net/contest/337673#problem/A]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector <int> table;
int m,n;
while(cin>>n>>m){
table.clear();
for(int i=0;i<2*n;i++) table.push_back(i);
int pos = 0;
for(int i=0;i<n;i++){
pos = (pos+m-1)%table.size();
table.erase(table.begin()+pos);
}
int j=0;
for(int i=0;i<2*n;i++){
if(!(i%50)&&i) cout<<endl;
if(j<table.size()&&i==table[j]){
j++;
cout<<"G";
}
else cout<<"B";
}
cout<<endl<<endl;
}
return 0;
}
  stack
hdu 1062 ”Text Reverse“

[https://vjudge.net/contest/337673#problem/B]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<stack>
using namespace std;
int main(){
int n;
char ch;
scanf("%d",&n);
ch = getchar();
while(n--){
stack<char>s;
while(true){
ch = getchar();
if(ch=='\n'||ch==' '||ch==EOF){
while(!s.empty()){
printf("%c",s.top());
s.pop();
}
if(ch=='\n'||ch==EOF) break;
printf(" ");
}
else s.push(ch);
}
printf("\n");
}
return 0;
}

hdu 1237 “简单计算器”

[https://vjudge.net/contest/337673#problem/C]:

思路:(太懒了8想写了..)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include<stack>
using namespace std;

int P(char c)
{
if (c == '+' || c == '-') return 1;
return 2;
}

double Ans(double x, double y, char c)
{
if (c == '+') return x + y;
if (c == '-') return x - y;
if (c == '*')return x*y;
return x / y;

}
int main() {
int n;
while (scanf("%d",&n)!=EOF)
{
char c = getchar();
if (c=='\n'&&n == 0)break;
stack<char> op;
stack<double>num;
num.push(n);
while (true)
{
scanf("%c %d", &c, &n);
char k = getchar();
while (!op.empty()&&P(c)<=P(op.top()))
{
char t = op.top();
op.pop();
double y = num.top();
num.pop();
double x = num.top();
num.pop();
double ans = Ans(x, y, t);
num.push(ans);
}
op.push(c);
num.push(n);
if (k == '\n')break;

}
while (!op.empty())
{
char t = op.top();
op.pop();
double y = num.top();
num.pop();
double x = num.top();
num.pop();
double ans = Ans(x, y, t);
num.push(ans);
}
printf("%.2f\n", num.top());
}
return 0;
}
  Queue
hdu 1702 ”ACboy needs your help again!“

[https://vjudge.net/contest/337673#problem/D]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main(){
int t,temp,n;
cin>>t;
while(t--){
string str1,str;
queue<int>Q;
stack<int>S;
cin>>n>>str;
for(int i=0;i<n;i++){
if(str=="FIFO"){
cin>>str1;
if(str1 == "IN"){
cin>>temp;
Q.push(temp);
}
if(str1 == "OUT"){
if(Q.empty()) cout<<"None"<<endl;
else {
cout<<Q.front()<<endl;
Q.pop();
}
}
}
else {
cin>>str1;
if(str1 == "IN"){
cin>>temp;
S.push(temp);
}
if(str1 == "OUT"){
if(S.empty()) cout<<"None"<<endl;
else {
cout<<S.top()<<endl;
S.pop();
}
}
}
}
}
return 0;
}

hdu 1873 ”看病要排队“

[https://vjudge.net/contest/337673#problem/E]:

注意队列中存放结构体的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct node{
int n,id;
}st;
bool operator < (const node&a,const node&b){
if(a.n==b.n) return a.id>b.id;
else return a.n<b.n;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
priority_queue<node> q[4];
char s[10];
int a,b;
int k=1;
for(int i=0;i<n;i++){
scanf("%s %d",s,&a);
if(strcmp(s,"IN")==0){
scanf("%d",&b);
st.n=b;
st.id=k++;
q[a].push(st);
}
else {
if(!q[a].empty()){
st = q[a].top();
q[a].pop();
printf("%d\n",st.id);
}
else printf("EMPTY\n");
}
}
}
return 0;
}
  list
hdu 1276 ”士兵队列训练问题“

[https://vjudge.net/contest/337673#problem/F]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<list>
using namespace std;
int main(){
int t,n;
cin>>t;
while(t--){
cin>>n;
int k = 2;
list<int>mylist;
list<int>::iterator it;
for(int i=1;i<=n;i++){
mylist.push_back(i);
}
while(mylist.size()>3){
int num = 1;
for(it = mylist.begin();it != mylist.end();){
if(num++%k==0) it = mylist.erase(it);
else it++;
}
k==2?k=3:k=2;
}
for(it =mylist.begin();it!=mylist.end();it++){
if(it!=mylist.begin()) cout<<" ";
cout<<*it;
}
cout<<endl;
}
return 0;
}
  set
hdu 2094 “产生冠军”

[https://vjudge.net/contest/337673#problem/G]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
set<string>A,B;
string s1,s2;
int n;
while(cin>>n&&n){
for(int i=0;i<n;i++){
cin>>s1>>s2;
A.insert(s1);
A.insert(s2);
B.insert(s2);
}
if(A.size()-B.size()==1) {
cout<<"Yes"<<endl;
}
else cout<<"No"<<endl;
A.clear();
B.clear();
}
return 0;
}
  map
hdu 2648 “Shopping”

[https://vjudge.net/contest/337673#problem/H]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
int n,m,p;
map<string,int>shop;
while(cin>>n){
string s;
for(int i=0;i<n;i++){
cin>>s;
}
cin>>m;
while(m--){
for(int i=1;i<=n;i++){
cin>>p>>s;
shop[s]+=p;
}
int rank= 1;
map<string,int>::iterator it;
for(it = shop.begin();it!=shop.end();it++){
if(it->second>shop["memory"]){
rank++;
}
}
cout<<rank<<endl;
}
shop.clear();
}
return 0;
}
  next_permutation
hdu 1027 "Ignatius and the Princess II"

[https://vjudge.net/contest/337673#problem/I]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[1001];
int n,m;
while(cin>>n>>m){
for(int i=1;i<=n;i++) a[i]=i;
int b = 1;
do{
if(m==b) break;
b++;
}while(next_permutation(a+1,a+1+n));
for(int i=1;i<n;i++){
cout<<a[i]<<" ";
}
cout<<a[n]<<endl;
}
return 0;
}

hdu 1716 “排列2”

[https://vjudge.net/contest/337673#problem/J]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
int a[5],tag=0;
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]))
{
if(a[0]==0 && a[1]==0 && a[2]==0 && a[3]==0)
break;
if(tag)
printf("\n");
tag=1;
int flag=1,tmp;
do
{
if(a[0]==0)
continue;
if(flag)
{
printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
flag=0;
}
else if(tmp==a[0])
printf(" %d%d%d%d",a[0],a[1],a[2],a[3]);
else
printf("\n%d%d%d%d",a[0],a[1],a[2],a[3]);
tmp=a[0];
}while(next_permutation(a,a+4));
printf("\n");
}
return 0;
}