URAL 1654. Cipher Message (STL stack)

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.