奇怪的键盘(2015蓝桥杯)

                                                     奇怪的键盘

                                                                     Time Limit: 1 Sec  Memory Limit: 128 MB
        
                                                                                    [Submit][Status][Discuss]

Description

近期科学家们在熬夜写论文报告,可是不争气的键盘出现了一系列的问题,有的时候点一下键盘往外出两个字符,有的时候只出一个。恰巧键盘的退格键坏了。
你知道科学家们都是有怪脾气的,他自己打出一篇文章,他就像恢复成原来的.
科学家满意的字符串是这样的,如果字符串中有相临的两个字符相同就消除这两个字符,形成一个新的字符串,如果新的字符串还有相邻的字符并且相同,再消除。一直到没有相邻的两个字符相同为止。

Input

一个字符串,长度不大于2*10^5,只包含小写字母

Output

令科学家满意的字符串。

Sample Input

reallazy

Sample Output

rezy

HINT

对于样例中的字符串这样变化的,reallazy -> reaazy -> rezy.

类似于括号匹配,用栈就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int MAXN=2e5+10;
char a[MAXN];
stack<char> st,q;
int main()
{
    gets(a);
    int len=strlen(a);
    for(int i=0;i<len;i++)
        if(st.empty()||st.top()!=a[i])
            st.push(a[i]);
        else
            st.pop();
    while(!st.empty())
    {
        q.push(st.top());
        st.pop();
    }
    while(!q.empty())
    {
        cout<<q.top();
        q.pop();
    }
    return 0;
}



原文地址:https://www.cnblogs.com/jk17211764/p/9677365.html