List-unidirectional linked list &&uva 11988 broken Keyboard (a.k.a.beijiu text) (broken keyboard (tragic text))
The new C + + 2011 Standard C++11 Adds the Forward_list,forward_list design goal to achieve comparable performance with the best handwritten unidirectional linked list data structures. Random access is not supported, no size operation. However, this container is very good in engineering, in the pursuit of higher time and space efficiency should be handwritten better, less time. The new standard library has a much faster container than the old one, and the new standard reservoir performance is almost certainly as good (and usually better) as the most carefully optimized homogeneous data structure. Modern C + + programs (of course, the same as in the pursuit of higher time and space efficiency should be better handwritten, time overhead is smaller) should use the standard library container. The reason is explained on page No. 470. From:c++primer 5th P (292-293)
You ' re typing a long text with a broken keyboard. Well it's not so badly broken. The only problemwith the keyboard was that sometimes the "home" key or the "end" key gets automatically pressed (internally) . You ' re not aware of this issue, since your ' re focusing on the text and do not even turn on themonitor! After your finished typing, you can see a text in the screen (if you turn on the monitor). In Chinese, we can call it beiju. Your task is to find the Beiju text.
InputThere is several test cases. Each test case was a single line containing at least one and at the most 100,000letters, underscores and both special characters ' [' and '] '. ' [' means the ' Home ' key is pressedinternally, and '] ' means the ' End ' key is pressed internally. The input is terminated by End-of-file (EOF).
OutputFor each case, print the Beiju text in the screen.
Sample Input This_is_a_[beiju]_text [[]][][]happy_birthday_to_tsinghua_university
Sample Output Beijuthis_is_a__text happy_birthday_to_tsinghua_university
SolutionThe solution comes from the book, which is annotated with your own understanding.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100000+5; One row per group of data, no more than 100,000 letters, underscores, "[", "]"
int LAST,CUR,NEXTT[MAXN]; The cursor is located behind the cur character, reference to next is ambiguous.
Char S[MAXN];
int main ()
{while
(scanf ("%s", s+1) ==1) {
int n=strlen (s+1);//input saved in s[1],s[2] In
last=cur=0;
nextt[0]=0;
for (int i=1;i<=n;i++) {
char ch=s[i];
if (ch== ' [') cur=0;
else if (ch== '] ') cur=last;
else{
Nextt[i]=nextt[cur]; The current cursor location encoding passed to Nextt did not encounter [formerly, here i=cur+1;
nextt[cur]=i;
if (cur==last) last=i; Update "last character" number
cur=i; Move cursor
}
for (int i=nextt[0];i!=0;i=nextt[i])
printf ("%c", S[i]);
printf ("\ n");
}
return 0;
}
Take a look at the picture
Thank you