List-unidirectional linked list &&uva 11988 broken Keyboard (a.k.a.beijiu text) (broken keyboard (tragic text)) Understanding and analysis

Source: Internet
Author: User
Tags data structures
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.