1654. Cipher Message
Time limit:1.0 Second
Memory limit:64 MB
Müller tried to catch Stierlitz red-handed many times, but alwaysfailed because Stierlitz could ever find some excuse. Once Stierlitz waslooking through his email messages. At this moment, Müller enteredsecretly and watched a meaningless sequence of symbols appear on the screen. " A cipher message, "Müller thought." UTF-8, "Stierlitz thought. It is known this Stierlitz ciphers messages by the following method. He deletes all spaces and punctuation marks. He replaces all successive identical letters by one such letter. He inserts and identical letters at a arbitrary place many times. Try to restore a message as it is after the second step. For this, remove fromthe message all pairs of identical letters inserted at the third step.InputThe only input line contains a message ciphered by Stierlitz. The messageconsists of lowercase 中文版 letters and its length are at most 200000.OutputOutput the restored message.Sample
| input |
Output |
Wwstdaadierfflitzzz |
Stierlitz |
problem Author:Vladimir Yakovlev (Idea by Alexander Klepinin)
problem Source:Neerc, Eastern subregion Quarterfinals
Parsing: Adjacent two identical letters can be eliminated, directly into the stack once, the same is out of the stack, different into the stack.
AC Code:
#include <bits/stdc++.h>
using namespace std;
Stack<char> p;
int main () {
#ifdef sxk
freopen ("In.txt", "R", stdin); freopen ("OUT.txt", "w", stdout);
#endif//Sxk
string s;
while (cin>>s) {
int len = S.size ();
for (int i=0; i<len; i++) {
if (!p.empty () && p.top () = = S[i]) p.pop ();
else P.push (S[i]);
}
s = "";
while (!p.empty ()) {
S + = P.top ();
P.pop ();
}
Reverse (S.begin (), S.end ());
cout<<s<<endl;
}
return 0;
}