Broken Keyboard UVA 11988 数组实现链表

这个构造十分巧妙,,,又学到一招,有点类似数组实现的邻接表

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <math.h>
 5 #define SIGMA_SIZE 26
 6 #pragma warning ( disable : 4996 )
 7 
 8 using namespace std;
 9 typedef long long LL;
10 
11 inline int Max(int a,int b) { return a>b?a:b; }
12 inline int Min(int a,int b) { return a>b?b:a; }
13 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); }
14 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; }  //a*b = gcd*lcm
15 const int inf = 0x3f3f3f3f;
16 const int maxn = 1e5+5;
17 const int maxk = 2e6+5;
18 
19 char str[maxn];
20 int nxt[maxn];
21 int cur, last;
22 
23 
24 void init()
25 {
26     memset( nxt, 0, sizeof(nxt) );
27     cur = last = 0;
28 }
29 int main()
30 {
31     while (~scanf("%s", str+1))
32     {
33         init();
34         int len = strlen(str+1);
35         nxt[0] = 0;
36 
37         for ( int i = 1; i <= len; i++ )
38         {
39             char ch = str[i];
40             if( ch == '[' ) cur = 0;
41             else if ( ch == ']' ) cur = last;
42             else {
43                 nxt[i] = nxt[cur];
44                 nxt[cur] = i;
45                 if( cur == last ) last = i;
46                 cur = i;
47             }
48         }
49         for( int i = nxt[0]; i; i = nxt[i] )
50             printf( "%c", str[i] );
51         printf( "
" );
52     }
53 }