The main idea: to change the order of a string after the output. Encounter "[" will be back to the front of the output, encountered "]" will be behind the contents of the final output.
Problem analysis: Using the Nxt[i] array to denote the subscript of the character after I, in fact, is to create a linked list with the character I as the head, similar to the chain-like forward star.
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace Std;char p[100005];int Nxt[100005],head,rear;int Main () { while (scanf ("%s", p+1)!=eof) { int l=strlen (p+ 1); head=rear=0; nxt[0]=0; for (int i=1;i<=l;++i) { if (p[i]== ' [') head=0; else if (p[i]== '] ') head=rear; else{ Nxt[i]=nxt[head]; nxt[head]=i; if (head==rear) rear=i; head=i; } } for (int i=nxt[0];i>0;i=nxt[i]) printf ("%c", P[i]); Puts (""); } return 0;}
This problem can also be solved by hand:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace Std;char p[100005];void print (int u) {for (int i=u;i>=0;--i) { if (p[i]== ' [')} {for (int j=i+1;j<=u;++ j) printf ("%c", P[j]); Print (i-1); break; } else if (p[i]== '] ') { print (i-1); for (int j=i+1;j<=u;++j) printf ("%c", P[j]); break; } if (i==0) { for (int j=0;j<=u;++j) printf ("%c", P[j]); } }} int main () { while (scanf ("%s", p)!=eof) { int l=strlen (p); Print (L-1); printf ("\ n"); } return 0;}
UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (linked list or recursive)