UVa 11988 Broken Keyboard(数组模拟链表)

题目链接:

https://cn.vjudge.net/problem/UVA-11988

 1 /*
 2 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键,表示光标跳到行末
 3 
 4 解题思路
 5 可以发现[]是可以嵌套的,采用纯模拟比较复杂,采用类似KMP中的next数组一样,记录一下每个字符的下一个字符位置,最后
 6 输出即可。 
 7 */ 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstring>
11 #include<list>
12 const int maxn=101000;
13 using namespace std;
14 
15 char str[maxn];
16 int nex[maxn];
17 int main()
18 {
19     int i,len,cur,las;
20     //freopen("E:\\testin.txt","r",stdin);
21     while(scanf("%s",str+1) != EOF){
22         len=strlen(str+1);
23         cur=las=0;
24         nex[0]=0;
25         for(i=1;i<=len;i++){
26             if(str[i] == '[')
27                 cur=0;
28             else if(str[i] == ']')
29                 cur=las;
30             else{
31                 nex[i]=nex[cur];
32                 nex[cur]=i;
33                 if(cur == las)    las = i;
34                 cur =i;
35             }
36         }
37         
38         for(i=nex[0];i != 0;i=nex[i]){
39             printf("%c",str[i]);
40         }
41         puts("");
42     }    
43     return 0;
44 } 

猜你喜欢

转载自www.cnblogs.com/wenzhixin/p/9129716.html