G-Gemstones 2019牛客暑假多校第八场 (模拟)

G:Gemstones

题意:给出一个字符串s( length <= 1e5 ),每有三个相同的相连就会像 消消乐一样消除。问最多消除多少次

思路:实际上就是模拟栈,每有三个相连就退栈

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 1e5+10;
int succ[maxn];
int ans;
string s;
int top;
int main(){ 
    cin>>s;
    {
        ans = top = 0;
        for(int i = 0;i<s.length();i++){
            if(top<2) succ[++top] = s[i];  
            else{
                succ[++top] = s[i];
                if(succ[top]==succ[top-1]&&succ[top-1]==succ[top-2]){
                    top -= 3;
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2019-08-18 16:22  Tianwell  阅读(142)  评论(0编辑  收藏  举报