吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)

吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A

题目:

思路:
  这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这题补了。这题很简单,看代码基本上就能看懂,就不解释了。

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 typedef long long ll;
17 typedef pair<ll, ll> pll;
18 typedef pair<ll, int> pli;
19 typedef pair<int, ll> pil;;
20 typedef pair<int, int> pii;
21 typedef unsigned long long ull;
22 
23 #define lson i<<1
24 #define rson i<<1|1
25 #define lowbit(x) x&(-x)
26 #define bug printf("*********
");
27 #define debug(x) cout<<"["<<x<<"]" <<endl;
28 #define FIN freopen("D://code//in.txt", "r", stdin);
29 #define IO ios::sync_with_stdio(false),cin.tie(0);
30 
31 const double eps = 1e-8;
32 const int mod = 1e9 + 7;
33 const int maxn = 1e6 + 7;
34 const double pi = acos(-1);
35 const int inf = 0x3f3f3f3f;
36 const ll INF = 0x3f3f3f3f3f3f3f3f;
37 
38 char s[105];
39 vector<char> v;
40 stack<char> p;
41 
42 int main() {
43     while(~scanf("%s", s)) {
44         v.clear();
45         while(!p.empty()) p.pop();
46         int len = strlen(s);
47         for(int i = 0; i < len; i++) {
48             if(p.empty()) {
49                 p.push(s[i]);
50                 continue;
51             }
52             if(p.top() == 'o' && s[i] == 'o') {
53                 p.pop();
54                 if(p.empty() || p.top() != 'O') {
55                     p.push('O');
56                 } else {
57                     p.pop();
58                 }
59             } else if(s[i] == 'O' && !p.empty() && p.top() == 'O') {
60                 p.pop();
61                 continue;
62             } else {
63                 p.push(s[i]);
64             }
65         }
66         while(!p.empty()) {
67             v.push_back(p.top());
68             p.pop();
69         }
70         reverse(v.begin(), v.end());
71         for(int i = 0; i < v.size(); i++) {
72             printf("%c", v[i]);
73         }
74         printf("
");
75     }
76     return 0;
77 }

Plug-in题目链接:http://codeforces.com/problemset/problem/81/A

题目:

题意:

  给你一个串,如果相邻两个字母相同,则将这两个字母消掉,如果消掉几个字母之后又有相邻得两个字母继续消掉。

思路:

  同上。

代码实现如下:

  

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <stack>
 5 #include <cmath>
 6 #include <bitset>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 typedef long long ll;
17 typedef pair<ll, ll> pll;
18 typedef pair<ll, int> pli;
19 typedef pair<int, ll> pil;;
20 typedef pair<int, int> pii;
21 typedef unsigned long long ull;
22 
23 #define lson i<<1
24 #define rson i<<1|1
25 #define lowbit(x) x&(-x)
26 #define bug printf("*********
");
27 #define debug(x) cout<<"["<<x<<"]" <<endl;
28 #define FIN freopen("D://code//in.txt", "r", stdin);
29 #define IO ios::sync_with_stdio(false),cin.tie(0);
30 
31 const double eps = 1e-8;
32 const int mod = 1e9 + 7;
33 const int maxn = 2e5 + 7;
34 const double pi = acos(-1);
35 const int inf = 0x3f3f3f3f;
36 const ll INF = 0x3f3f3f3f3f3f3f3f;
37 
38 char s[maxn];
39 vector<char> v;
40 stack<char> q;
41 
42 int main() {
43     scanf("%s", s);
44     q.push(s[0]);
45     int len = strlen(s);
46     for(int i = 1; i < len; i++) {
47         if(q.empty()) {q.push(s[i]);continue;}
48         if(s[i] == q.top()) {
49             q.pop();
50         } else {
51             q.push(s[i]);
52         }
53     }
54     while(!q.empty()) {
55         v.push_back(q.top());
56         q.pop();
57     }
58     reverse(v.begin(), v.end());
59     for(int i = 0; i < v.size(); i++) {
60         printf("%c", v[i]);
61     }
62     printf("
");
63     return 0;
64 }
原文地址:https://www.cnblogs.com/Dillonh/p/9508039.html