题目链接
已知字符串str。字符串str包含字符’x’,’y’。 如果相邻的两个字符不同,消除两个字符,优先从左边进行消除。 xyyx - > yx ->
可以用一个栈来模拟
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char a[1010];
bool st[N];
int main()
{
scanf("%s",a+1);
int l=strlen(a+1);
stack<char> q,k;
char pre=a[1];
q.push(a[1]);
for(int i=2;i<=l;i++)
{
if(q.size()==0)
{
q.push(a[i]);
continue;
}
char t=q.top();
if(a[i]==t) q.push(a[i]);
else q.pop();
}
while(q.size())
{
char t=q.top();
k.push(t);
q.pop();
}
while(k.size())
{
printf("%c",k.top());
k.pop();
}
}