UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (linked list or recursive)

Source: Internet
Author: User

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)

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.