11988:Broken Keyboard (a.k.a. Beiju Text)

Broken Keyboard (a.k.a. Beiju Text)


#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
char s[maxn];
int last,cur,Next[maxn];//光标在cur右边
int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    while(scanf("%s",s+1) == 1){
        int n = strlen(s+1);
        last = cur = 0;
        Next[0] = 0;
        for(int i = 1;i <= n;i++){
            if(s[i] == '[') cur = 0;
            else if(s[i] == ']') cur = last;
            else{
                Next[i] = Next[cur];
                Next[cur] = i;
                if(cur == last) last = i;//更新last
                cur = i;
            }
        }
        for(int i = Next[0];i != 0;i = Next[i]) putchar(s[i]);
        putchar('\n');
    }
    return 0;
}

posted @ 2018-05-23 11:59  ACLJW  阅读(113)  评论(0编辑  收藏  举报