UVA - 11988

You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problemwith 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 themonitor! 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,000letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressedinternally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file(EOF).

Output

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

Sample Input

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

Sample Output

BeijuThis_is_a__textHappy_Birthday_to_Tsinghua_University

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char ch[101000];
int main()
{
	while(~scanf("%s", ch+1))
	{
		int Rindex[101000] = {0};
		int len = strlen(ch+1);
		int cur = 0, last = 0;
		Rindex[0] = 0;
		for(int i = 1 ; i <= len; ++i)
		{
			if(ch[i] == '[')
				cur = 0;
			else if(ch[i] == ']')
			{
				cur = last;
			}
			else
			{
				Rindex[i] = Rindex[cur];
				Rindex[cur] = i;
				if(cur == last)
					last = i;
				cur = i;
			}
		}
		for(int i = Rindex[0]; i ; i = Rindex[i])
            printf("%c",ch[i]);
        printf("\n");
        memset(ch,0,sizeof(ch));
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章