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). 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__text
Happy_Birthday_to_Tsinghua_University
题目大意
您正在键入破坏的键盘输入长文本。好吧,它没有那么糟糕。键盘的唯一问题是有时“主页”键或“结束”键会自动按下(内部)。
你不知道这个问题,因为你专注于文本,甚至没有打开显示器!键入完成后,您可以在屏幕上看到文本(如果打开显示器)。在中文里,我们可以称它为Beiju。你的任务是找到Beiju文本。
输入
有几个测试用例。每个测试用例是一行,包含至少一个,最多100,000个字母,下划线和两个特殊字符'['和']'。 '['表示内部按下“Home”键,']表示内部按下“End”键。输入由文件结束(EOF)终止。
输出
对于每种情况,请在屏幕上打印Beiju文本。
其实就是一个简单的链表运用。。结果WA了好几次,最后总算是AC了
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn =(1000000);
int main()
{
char a[maxn];
int next[maxn];
while(scanf("%s",a+1)!=EOF)
{
int len=strlen(a+1);
next[0]=0;
int end=0;
int cur=0;
for(int i=1;i<=len;i++)
{
char temp=a[i];
if(a[i]=='[')
cur=0;
else if(a[i]==']')
cur=end;
else
{
next[i]=next[cur];
next[cur]=i;
if(cur==end)
end=i;
cur=i;
}
}
for(int i=next[0];i!=0;i=next[i])
printf("%c",a[i]);
printf("\n");
}
return 0;
}