2019暑期集训牛客多校8-G.Gemstones
题解
栈的简单应用,类似于删括号,碰到连续相同的三个元素就删掉并计数。
题解
#include<iostream>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
struct node
{
int cot;
char ch;
};
node p,t;
stack<node> st;
char s[MAXN];
int main()
{
int ans=0;
scanf("%s",s);
int len=strlen(s);
while(!st.empty())st.pop();
for(int i=0;i<len;i++)
{
p.ch=s[i];
if(!st.empty()&&st.top().ch==p.ch)
{
p.cot=1+st.top().cot;
st.pop();
if(p.cot<3)st.push(p);
if(p.cot==3)ans++;
}
else
{
p.cot=1;
st.push(p);
}
}
cout<<ans<<endl;
}