STL training (uva上一些比较好的用来熟悉STL)

题目都比较简单这里只给题号和代码:

uva 141, uva 11988, uva 10700, uva 10954, uva 10887, uva 10391, uva 10282, uva 673

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 set<string> ocr;
10 int n;
11 
12 string MS(int lc[][51])
13 {
14     string ret = "";
15     for(int i=0; i<n; i++)
16     for(int j=0; j<n; j++){
17         ret = ret+char(lc[i][j]+'0');
18     }
19     return ret;
20 }
21 
22 void spin(int lc[][51]){
23     int temp[51][51] ={0};
24     for(int i=0; i<4; i++){
25         for(int j=0; j<n; j++){
26             for(int k=0; k<n; k++){
27                 temp[k][n-1-j] = lc[j][k];
28             }
29         }
30         memcpy(lc, temp, sizeof temp);
31         ocr.insert(MS(temp));
32     }
33 
34 }
35 
36 int main()
37 {
38 //    freopen("in.txt", "r", stdin);
39 
40     int a, b, lc[51][51], isans;
41     char op;
42     while(scanf("%d", &n)!=EOF && n)
43     {
44         memset(lc, 0, sizeof lc);
45         ocr.clear();isans = 0;
46         ocr.insert(MS(lc));
47         for(int i=0; i<2*n; i++){
48             scanf("%d%d", &a, &b);cin >> op;
49             if(isans)continue;
50             a--;b--;
51             if(op=='-')lc[a][b] = 0;
52             else lc[a][b] = 1;
53 
54             if(ocr.count(MS(lc))!=0){
55                 if(i%2==0)printf("Player 2 wins on move %d\n", i+1);
56                 else printf("Player 1 wins on move %d\n", i+1);
57                 isans = 1;
58             }else spin(lc);
59         }
60         if(!isans)printf("Draw\n");
61     }
62     return 0;
63 }
uva141
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string.h>
 4 #include <list>
 5 using namespace std;
 6 char str[1000100];
 7 int main()
 8 {
 9     while(gets(str)){
10         list<char> l;
11         list<char>::iterator p = l.begin();
12         int len = strlen(str);
13         for(int i=0; i<len; i++){
14             if(str[i]=='[')p = l.begin();
15             else if(str[i]==']')p = l.end();
16             else l.insert(p, str[i]);
17         }
18         for(p = l.begin(); p!=l.end(); ++p)cout << *p;
19         cout << endl;
20     }
21     return 0;
22 }
uva11988
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 
 5 using namespace std;
 6 
 7 typedef long long ll;
 8 stack<ll> ls, bs;
 9 
10 void work(int op)
11 {
12     if(op==1){
13         ll temp = bs.top();bs.pop();
14         temp += bs.top();bs.pop();
15         bs.push(temp);
16     }
17     if(op==2){
18         ll temp = ls.top();ls.pop();
19         temp *= ls.top();ls.pop();
20         ls.push(temp);
21     }
22 }
23 
24 int main()
25 {
26 //    freopen("in.txt", "r", stdin);
27 
28     int T;char c;
29     scanf("%d", &T);
30     getchar();
31     while(T--)
32     {
33         ll minans = 0, maxans = 1, num = 0, op=-1;
34         while(c = getchar()){
35             if(c=='\n')break;
36             if(c=='+' || c=='*'){
37                 ls.push(num);bs.push(num);
38                 num = 0;
39                 work(op);
40                 if(c=='+')op = 1;
41                 else op = 2;
42                 continue;
43             }
44             num*=10;
45             num+=(c-'0');
46         }
47         ls.push(num);bs.push(num);
48         work(op);
49         while(!ls.empty()){minans += ls.top();ls.pop();}
50         while(!bs.empty()){maxans *= bs.top();bs.pop();}
51         printf("The maximum and minimum are %lld and %lld.\n", maxans, minans);
52     }
53     return 0;
54 }
uva10700
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <vector>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 //    freopen("in.txt", "r", stdin);
10 
11     int n, num;
12     while(scanf("%d", &n)!=EOF && n){
13         priority_queue<int, vector<int>, greater<int> > q;
14         for(int i=0; i<n; i++){
15             cin >> num;
16             q.push(num);
17         }
18         int ans = 0;
19         while(!q.empty()){
20             num = q.top(); q.pop();
21             if(q.empty())break;
22             num += q.top(); q.pop();
23             ans+=num; q.push(num);
24         }
25         cout << ans << endl;
26     }
27     return 0;
28 }
uva10954
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <set>
 4 
 5 using namespace std;
 6 
 7 const int LEN = 1510;
 8 
 9 int main()
10 {
11 //    freopen("in.txt", "r", stdin);
12 
13     int T, n, m;
14     char ss[LEN];
15     set<string> dir;
16     string str[LEN], buf;
17     cin >> T;
18     for(int kase = 1; T--; kase++){
19         cin >> n >> m ;
20         getchar();
21         dir.clear();
22         for(int i=0; i<n; i++){
23             gets(ss);
24             str[i] = ss;
25         }
26         for(int j=0; j<m; j++){
27             gets(ss);
28             buf = ss;
29             for(int j=0; j<n; j++)dir.insert(str[j]+buf);
30         }
31         printf("Case %d: %d\n", kase, dir.size());
32     }
33     return 0;
34 }
uva10887
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 
 6 using namespace std;
 7 
 8 const int LEN = 120001;
 9 set<string> dic;
10 char str[LEN][50];
11 
12 bool Judge(int x, int p, int len){
13     char a[50], b[50], pa = 0, pb = 0;
14     for(int i=0; i<len; i++){
15         if(i<p)a[pa++] = str[x][i];
16         else b[pb++] = str[x][i];
17     }
18     a[pa] = b[pb] = 0;
19     if(dic.find(a)!=dic.end() && dic.find(b)!=dic.end())return true;
20     else return false;
21 }
22 
23 int main()
24 {
25 //    freopen("in.txt", "r", stdin);
26     int len = 0;
27     while(scanf("%s", str[len])!=EOF){
28         dic.insert(str[len]);
29         len ++;
30     }
31     for(int i=0; i<len; i++){
32         int l = strlen(str[i]);
33         for(int j=1; j<l-1; j++){
34             if(Judge(i, j, l)){
35                 cout << str[i] << endl;
36                 break;
37             }
38         }
39     }
40     return 0;
41 }
uva10391
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 //    freopen("in.txt", "r", stdin);
10 
11     char a[21], b[21];
12     map<string, string> dic;
13     while(scanf("%s", a)!=EOF){
14         if(getchar()=='\n')break;
15         scanf("%s", b);
16         dic[b] = a;
17     }
18     cout << dic[a] << endl;
19     while(scanf("%s", a)!=EOF)
20     {
21         if(dic.count(a))cout << dic[a] << endl;
22         else cout << "eh" << endl;
23     }
24     return 0;
25 }
uva10282
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 //    freopen("in.txt", "r", stdin);
10 
11     int T;
12     char str[10100];
13     cin >> T;
14     getchar();
15     while(T--){
16         stack<char> s;
17         int ans = 1, len, i=0;
18         gets(str);
19         len = strlen(str);
20         while(i<len){
21             str[i] = str[i];
22             if(str[i]=='[' || str[i]=='(')s.push(str[i]);
23             else if(str[i]==']' && !s.empty() && s.top()=='[')s.pop();
24             else if(str[i]==')' && !s.empty() && s.top()=='(')s.pop();
25             else {ans = 0;break;}
26             i++;
27         }
28         if(!s.empty())ans = 0;
29         if(ans)cout << "Yes" << endl;
30         else cout << "No" << endl;
31     }
32 }
uva673

 

posted @ 2014-01-14 22:09  张小豪  阅读(427)  评论(0编辑  收藏  举报