Test instructions
Enter a line of characters that contains ' [' and '] ', which means the home and end keys on the keyboard, and then the analog characters are entered on the keyboard. Enter the final result of a line
Analysis:
Using an array to simulate a linked list, in the list of the ends of the insertion of letters and then output, the method needs more practice to practice, in fact, a bit like the adjacency table.
For convenience, a virtual node is often placed before the first element of the list.
#include <bits/stdc++.h>using namespacestd;Const intMAXL =100000+7;intMain () {CharSTR[MAXL]; intcur, last; intNEXT[MAXL];//Next[index] is str[index] The next letter is Str[next[index]]//for convenience, a virtual node is often placed before the first element of the list. So start from Str+1 . while(~SCANF ("%s", str+1)) { intLen = strlen (str+1); next[0] =0;//start next point to the virtual nodecur = last =0; for(inti =1; I <= Len; i++){ if(Str[i] = ='[') {cur=0; } //cursor is behind cur cur...| Else if(Str[i] = =']') {cur=Last ; } Else{Next[i]=Next[cur]; Next[cur]=i; /*Note that these two sentences when cur = 0, i = 1 o'clock, next[1] = next[0]------------to connect the virtual node to next[1] next[0] = 1 ------------take the next 0 to 1, which means the next0 shifts to Next1 next0 to 1 .*/ if(cur = = last) last =i; Cur=i; } } for(inti = next[0]; I! =0; i =Next[i]) {printf ("%c", Str[i]); } puts (""); }}
UVa 11998 broken Keyboard (array implementation linked list)