UVa11988-破损的键盘 Broken Keyboard
题目描述
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
分析
终于搞懂了链表的用法...之前还是觉得很难。
又是证明了这点读程序需要异常强大的耐心...反复的读,写出步骤,画出示意图,意思也就出来了。
代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=100000+5; 6 int last,cur,next[maxn]; 7 char s[maxn]; 8 int main() 9 { 10 while(cin>>s+1)//(scanf("%s",s+1)==1) 11 { 12 13 int n=strlen(s+1); 14 /*for(int i=0;i<n;i++) 15 { 16 cout<<s[i]<<" "; 17 }*/ 18 last=cur=0; 19 next[0]=0; 20 for(int i=1;i<=n;i++) 21 { 22 char ch=s[i]; 23 if(ch=='[')cur=0; 24 else if(ch==']')cur=last; 25 else{ 26 next[i]=next[cur]; 27 next[cur]=i; 28 if(cur==last) last=i; 29 cur=i; 30 } 31 } 32 for(int i=next[0];i!=0;i=next[i]) 33 cout<<s[i]; 34 cout<<endl; 35 } 36 return 0; 37 }