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

题目:

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

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

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

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; char s[];
vector<char> v;
stack<char> p; int main() {
while(~scanf("%s", s)) {
v.clear();
while(!p.empty()) p.pop();
int len = strlen(s);
for(int i = ; i < len; i++) {
if(p.empty()) {
p.push(s[i]);
continue;
}
if(p.top() == 'o' && s[i] == 'o') {
p.pop();
if(p.empty() || p.top() != 'O') {
p.push('O');
} else {
p.pop();
}
} else if(s[i] == 'O' && !p.empty() && p.top() == 'O') {
p.pop();
continue;
} else {
p.push(s[i]);
}
}
while(!p.empty()) {
v.push_back(p.top());
p.pop();
}
reverse(v.begin(), v.end());
for(int i = ; i < v.size(); i++) {
printf("%c", v[i]);
}
printf("\n");
}
return ;
}

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

题目:

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

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

题意:

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

思路:

  同上。

代码实现如下:

  

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; char s[maxn];
vector<char> v;
stack<char> q; int main() {
scanf("%s", s);
q.push(s[]);
int len = strlen(s);
for(int i = ; i < len; i++) {
if(q.empty()) {q.push(s[i]);continue;}
if(s[i] == q.top()) {
q.pop();
} else {
q.push(s[i]);
}
}
while(!q.empty()) {
v.push_back(q.top());
q.pop();
}
reverse(v.begin(), v.end());
for(int i = ; i < v.size(); i++) {
printf("%c", v[i]);
}
printf("\n");
return ;
}
05-06 04:20