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

题目复制太麻烦了,甩个链接

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693

直接模拟光标操作时间复杂度较高,所以用链表存储顺序。

pos表示光标位置

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 char s[200000];
 8 char ch;
 9 int last,pos;//last存储最后一个字符的位置,pos存储当前光标位置 
10 int nexto[200000];
11 int main(){
12     while(scanf("%s",s+1)!=EOF){
13         int len=strlen(s+1);
14         last=0;pos=0;
15         nexto[0]=0;//初始化 
16         int i;
17         for(i=1;i<=len;i++){
18             if(s[i]=='[')pos=0;
19             else if(s[i]==']')pos=last;
20             else{
21                 nexto[i]=nexto[pos];
22                 nexto[pos]=i;
23                 if(pos==last)last=i;//更新最后位置
24                 pos=i;//更新光标位置 
25             }
26         }
27         for(i=nexto[0];i!=0;i=nexto[i])printf("%c",s[i]);
28         printf("
");
29     }
30     return 0;
31 }