[牛客暑期多校第八场][栈]G-Gemstones

发布于 2019-08-12  697 次阅读


https://ac.nowcoder.com/acm/contest/888/G

给一个字符串 3个连续且相同的能够消去 问这个字符串最多消去几次
直接模拟栈 入栈之和如果发现和栈下面的两个都相同 这三个都出栈
注意不要用stl的栈 因为不好找stack[top--] 手写一个栈就好
代码抄的咖啡鸡的 圈粉了 代码写的真好看

#include<algorithm>
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<ctime>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<cstring>
using namespace std;
const int maxn = 5e5 + 5;
typedef long long ll;

char s[maxn], t[maxn];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	scanf("%s", s);
	int n = strlen(s);
	int ans = 0;
	int tmp = 0;
	for (int i = 0; i < n; i++) {
		t[++tmp] = s[i];
		if (t[tmp] == t[tmp - 1] && t[tmp] == t[tmp - 2]) {
			ans++;
			tmp -= 3;
		}
	}
	cout << ans << endl;
	return 0;
}
//1 1 2 3 1+1+1+1+1+2+2+2+3+3 
//1 2 1 3 1+1+1+1+2+2+2+2+3+3

愿风指引你的道路,愿你的刀刃永远锋利。