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
在输入一个串的时候可能会按到home键和end键,[代表home键,]代表end键,输出最终的串。
设head为整个串的头,tail为尾,找到第一个不是[]的位置,head和tail都初始化为这个位置。flag=1代表按了home键,在前面输入字符,flag=0代表按了end,在后面输入字符。用t记录在前面插入字符时应插入的位置。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include<algorithm>
#include <map>
#include<stack>
#include<queue>
#define MAXN 100010
using namespace std;
char str[MAXN];
int next[MAXN];
int main(){
freopen("in.txt","r",stdin);
while(scanf("%s",str)!=EOF){
int head=-1,tail=-1,flag=0,t;
memset(next,-1,sizeof(next));
for(int i=0;i<strlen(str);i++) if(str[i]!='['&&str[i]!=']'){
head=tail=i;
break;
}
for(int i=head+1;i<strlen(str);i++){
if(str[i]=='['){
next[i]=head;
head=i;
t=i;
flag=1;
}
else if(str[i]==']'){
next[tail]=i;
tail=i;
flag=0;
}
else{
if(flag){
next[i]=next[t];
next[t]=i;
t=i;
}
else{
next[tail]=i;
tail=i;
}
}
}
for(int i=head;i!=-1;i=next[i]) if(str[i]!='['&&str[i]!=']') printf("%c",str[i]);
puts("");
}
return 0;
}