Plug-in CodeForces

  题意就是给你一个串,然后要求删除相邻且相等的2个字符,直到没有相邻且相等的2个字符为止。

  这题就是用栈这个容器来模拟删除相邻且相等的2个字符这个过程。最后栈里装的即是答案。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2*1e5+10;
stack<char>s;
char str[maxn];
int main()
{
    while(!s.empty()) s.pop();
    int len;
    gets(str+1);
    len=strlen(str+1);
    for(int i=len;i>=1;i--)//反着删除是为了最后输出答案是方便,因为栈是后入先出的数据结构
    {
        if(!s.size())
        {
            s.push(str[i]);
        }
        else
        {
            if(s.top()==str[i])
            {
                s.pop();
            }
            else
            {
                s.push(str[i]);
            }
        }
    }
    while(!s.empty())
    {
        cout<<s.top();
        s.pop();
    }
    cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/eason9906/p/11754929.html