17999 Light-bot Simulation + KMP to find the circulation section

Source: Internet
Author: User

Http://acm.scau.edu.cn:8000/uoj/mainMenu.html

17999 Light-bot

Time limit: 1000MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0

Question types: programming language: not Limited

Description

I (You needn ' t knowwho am "I".) am currently playing a game called "Light-bot". In the game, the ' Light-bot ' is controlled

by a program. Theprogram includes:

(1) The mainprocedure. The main procedure is the entrance of the program, same as the ' main ' in C/C + +.

(2) Sub procedure#1. Sub procedure.

(3) Sub procedure#2. Sub procedure No.2.

Note:if a subprocedure ends, it'll return to the command next to it's calling place.

Here, we suggestthat a alphabetical letter stands for an ACTION COMMAND excluding ' P ' and ' P '.

So, "Light-bot" would begin executing from the first command in the Mainprocedure. Once it meets with a letter ' P ', it would call Sub

Procedure #1, Whilea letter ' P ' indicates-to-call Sub procedure #2. The main procedure, procedure#1 and procedure #2 can call

Procedure #1 orprocedure #2 freely. It means that recursive calls is possible.

Now, I just want toknow given a program, what's the Nth ACTION COMMAND Light-bot would execute.




Input format

The first line ofthe input contains an integer t (t <=), indicating there is t cases inthe input file.

For each test case,the first line is the main procedure. The second one is Sub procedure #1 andthe was a Sub procedure #2. each

Procedure ends witha ' # ' sign, which was not considered a command. The length of a part would notexceed 10.

And on the nextline, there is one integer n (1 <= n <= 108), indicates Theorder I ask. It is guaranteed this there must be an ACTION COMMAND

Fitting the requirement.

Please see theexample for more details.



Output format

For each case,print one line, the ACTION COMMAND is fits the description.




Input sample

4

abcdp#

pefg#

hijk#

4

abcdp#

pefg#

hijk#

5

abcdp#

pefg#

hijk#

9

abcdp#

efghp#

#

12




Output sample

D

H

E

H




Source

Lrc_seraph

First, because the maximum number is 1000 (not loop)

Then I can force the simulation 2000 times and then get a sequence. The back of this sequence must be circular.

Is XXXXABCABCABC .... Such

You can then reverse KMP once, in order to find the cycle section, starting from the 100th item,

The reasons are:

1. The 100th item begins with the same length of the circular section,

2, to prevent the AAAA these false circulation section interference.

When I was a long-time simulation, I simulated up to up, but I took an equal sign and simulated up + 1 steps. And then always WA

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Charstr[4][ -];Charma[ -];intlen[5];intLenma;Const intup = -+ -;CharAll[up + -];intLenall =0;CharSub[up + -];intLensub =0;voidDfsintNowintcur) {    if(Lenall >= up)return;  for(inti = cur; I <= Len[now] && Lenall < up; ++i) {if(Str[now][i] = ='P') {DFS (0,1); } Else if(Str[now][i] = ='P') {DFS (1,1); } ElseAll[++lenall] =Str[now][i]; }}intTonext[up + -];voidKMP () {inti =1, j =0; tonext[1] =0;  while(I <=lensub) {        if(J = =0|| Sub[i] = =Sub[j]) {tonext[++i] = + +J; } Elsej =Tonext[j]; }}voidWork () {scanf ("%s", Ma +1);  for(inti =0; I <=1; ++i) {scanf ("%s", Str[i] +1); Len[i]= strlen (Str[i] +1); Len[i]--; } Lenma= strlen (Ma +1); Lenma--; Lenall=0;  for(inti =1; I <= Lenma && Lenall < up; ++i) {//this up can't go to the equals sign.        if(Ma[i] = ='P') {DFS (0,1); } Else if(Ma[i] = ='P') {DFS (1,1); } Else{all[++lenall] =Ma[i]; }} All[lenall+1] =' /'; intVal; scanf ("%d", &val); if(Val <=Up ) {printf ("%c\n", All[val]); return; } lensub=0;  for(inti = Lenall; I >=1; --i) {sub[++lensub] =All[i]; } sub[lensub+1] =' /'; KMP ();//cout << Sub + 1 << endl;    intCIR =0;//cout << all + 1 << endl;     for(inti = ++ -; I <= lensub; ++i) {if(Tonext[i +1] ==1)Continue; intt = i-(tonext[i +1] -1); if(i% t = =0) {Cir=T;//cout << i << Endl;             Break; }    }//cout << Cir << Endl;    if(Cir = =0) while(1); intleft = Val-Up ; Left%=Cir; if(left = =0) left =Cir; intPoint = Lenall-cir +Left ; printf ("%c\n", All[point]);}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif    intT; scanf ("%d", &t);  while(t--) work (); return 0;}
View Code

17999 Light-bot Simulation + KMP to find the circulation section

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.