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; }
转载于:https://www.cnblogs.com/Tianwell/p/11372779.html
相关资源:JAVA上百实例源码以及开源项目