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

Problem B

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

You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (internally).

You're not aware of this issue, since you're focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

In Chinese, we can call it Beiju. Your task is to find the Beiju text.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters '[' and ']'. '[' means the "Home" key is pressed internally, and ']' means the "End" key is pressed internally. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each case, print the Beiju text on the screen.

Sample Input

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

Output for the Sample Input

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <stack>
12 #include <sstream>
13 #include <cctype>
14 #include <cassert>
15 #include <typeinfo>
16 #include <utility>    //std::move()
17 using namespace std;
18 const int INF = 0x7fffffff;
19 const double EXP = 1e-8;
20 const int MS = 100005;
21 typedef long long LL;
22 int last, cur, nexti[MS];    //next[0-n]  0为虚拟节点,也是结束标记    链表法
23 char str[MS];
24 
25 int main()
26 {
27     while (scanf("%s", str + 1) == 1)
28     {
29         int len = strlen(str + 1);
30         last = cur = 0;     //  在0的后面增加下一个元素
31         nexti[0] = 0; 
32         for (int i = 1; i <= len; i++)
33         {
34             if (str[i] == '[')
35                 cur = 0;//   光标移动0这,在0的后面添加下一个元素
36             else if (str[i] == ']')
37                 cur = last;
38             else
39             {
40                 nexti[i] = nexti[cur];
41                 nexti[cur] = i;
42                 if (cur == last)
43                     last = i;
44                 cur = i;
45             }
46         }
47         for (int i = nexti[0]; i != 0; i = nexti[i])
48             printf("%c", str[i]);
49         printf("\n");
50     }
51     return 0;
52 }

 

posted @ 2015-02-01 12:09  daydaycode  阅读(252)  评论(0编辑  收藏  举报