Array simulation One-way list example (UVa11988)

Source: Internet
Author: User

The list of pointers is implemented by means of the next node of the current node, which is a for (int i=next[0];i!=0;i=next[i]) i=next[i];

Examples:

You have a broken keyboard. All keys on the keyboard work correctly, but sometimes the home or end keys are automatically pressed. You didn't know the keyboard was the problem, but focused on the script, even the monitor didn't open. When you turn on the monitor, a tragic text is displayed in front of you. Your task calculates this tragic text before opening the display.

The input contains multiple sets of data. Each group contains a row of data, containing no more than 100,000 letters, underscores, characters "[" or "]". where "[" means home, "]" means the end key. The input end flag is the file Terminator (EOF).

#include <cstdio>#include<cstring>Const intmaxn=100000+5;intLAST,CUR,NEXT[MAXN];CharS[MAXN];intMain () { while(SCANF ("%s", s+1)==1) {        intN=strlen (s+1); Last=cur=0; next[0]=0;  for(intI=1; i<=n;i++) {                CharCh=S[i]; if(ch=='[') cur=0; if(ch==']') cur=Last ; Else{Next[i]=Next[cur]; Next[cur]=i; if(cur==last) last=i; Cur=i; }        }         for(inti=next[0];i!=0; i=Next[i]) printf ("%c", S[i]); printf ("\ n"); }return 0;}

Cur represents the position of the current character, that is, it is on the left side of the cursor, I represents the next character Next[i]=next[cur]; is the position of the next character is assigned to 0,next[cur]=i; it is cur and I link up,cur->i; In the absence of "[" or "]", last in front of the cur, that is, the front of the current character, so when S[i] is "[" is, last holds its previous character position, when "]", last=cur again let cur back to that position, then cur and I link.

Take this input as an example.

Abc[de]f

First in ABC, the 0->1->2->3 is sequentially linked when encountering ' [', cur=0; so next[5]=next[0]; next[0]=5; so the order becomes 0->5->1->2-> 3; Then to e this character, likewise next[6]=next[5];next[5]=6; So the order becomes 0->5->6->1->2->3;. And then to '] ' this time cur=last=3; And then to the F; NEXT[9]=NEXT[3]; next[3]=9; So the final order is 0->5->6->1->2->3->9; This is also the final printing order, note that the beginning of the 0 is only the beginning, corresponding to the pointer in the head pointer, which itself does not represent any character because S[0] does not represent the character, S is a character starting from 1. Of course, the same as the list of the 0 can have, no also line, if not from the beginning of 0 need to set next[1]=0; Then, the last individual printing s[1];

The core is next[i]=next[cur] equivalent to p->next=*l->next; in pointer creation (one for next[i]=0, one for p->next=null) Next[cur]=i equivalent to pointer creation * l->next=p; (a let cur->i a let L's next->p)

Array simulation One-way list example (UVa11988)

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.