破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)

破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)

题意描述

你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下。你却不知道此问题,而是专心致志地打稿子,甚至显示器都没开。当你打开显示器之后,展现你面前的数一段悲剧文本。你的任务是在显示器打开前计算出这段悲剧的文本。 给你一段按键的文本,其中'['表示Home键,']'表示End键,输入结束标志是文件结束符(EOF)。

样例输入

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

样例输出

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

实现

#include<bits/stdc++.h>
using namespace std;
int main(){
    string line;
    while(getline(cin,line)){
        deque<string> result;
        string str;
        int flag=1;//flag=1 push_back,flag=0 push_front
        for(int i=0;i<line.length();i++){
            if(line[i] == '['){flag=0;}
            else if(line[i] == ']'){
                    if(!str.empty()){
                        result.push_front(str);
                        str.clear();
                    }
                    flag=1;}
            else{//normal c or _
                if(flag){
                    string s;
                    s.push_back(line[i]);
                    result.push_back(s);
                }
                else {
                    str=str+line[i];
                }
            }
        }
        deque<string>::iterator it;
        for(it=result.begin();it!=result.end();++it){
            cout<<*it;
        }
        cout<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/MarkKobs-blog/p/10464375.html