Sunday, August 17, 2014

uva : 245 - Uncompress


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<vector>
#include<map>
#include<iostream>
using namespace std;
int main()
{
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    char a[100000],ch;
    int n=0;
    map<string,int>wordchecking;
    vector<string>word;
    string s;
    while(scanf("%c",&ch)==1)
    {
        if(ch>='0'&&ch<='9')
        {
            int number=0;
            number=ch-'0';
            while(scanf("%c",&ch)==1)
            {

                if(ch>='0'&&ch<='9')
                number=number*10+(ch-'0');
                else break;
            }if(number==0)break;
            int find=word.size()-number;
            s=word[find];
            cout<<s;
            word.erase(word.begin()+find);
            word.push_back(s);
            //cout<<word[find];
            if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
            {
                a[n++]=ch;
            }
            printf("%c",ch);
        }
        else{
            printf("%c",ch);
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
        {
            a[n++]=ch;
        }
        else
        {
            a[n]='\0';
            s=a;
            if(strlen(a)>0){
            if(wordchecking[s]==0)
            {
                word.push_back(s);
                wordchecking[s]=word.size();
            }
            else if(wordchecking[s]>0)
            {
                word.erase (word.begin()+(wordchecking[s]-1));
                word.push_back(s);
            }
            }
            n=0;
        }
        }
    }
    return 0;
}

No comments:

Post a Comment