牛客多校第八场 G Gemstones 栈/贪心

题意:

对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组。

题解:

直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, LL>P;
const int M = 2e3 + 5;
const LL mod = 998244353;
const LL lINF = 0x3f3f3f3f3f3f3f3f;
#define ls (rt<<1)
#define rs (rt<<1|1)
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
string ss;
int flag;
int cnt;
int main()
{
    cin >> ss;
    flag = 1;
    while (flag)
    {
        flag = 0;
        for (int i = 2; i < ss.size(); i++)
        {
            if (i < 2)
                continue;
            if (ss[i - 2] == ss[i - 1] && ss[i] == ss[i - 1])
            {
                ss.erase(i - 2, 3);
                flag = 1;
                i -= 3;
                cnt++;
            }
        }
    }
    printf("%d
", cnt);
}
原文地址:https://www.cnblogs.com/isakovsky/p/11348541.html