Start with the question:
| F. Dancing the cheeky-Cheeky |
Context
The cheeky-Cheeky is a new song. they dance it in Mula, and also in Hong Kong. all the freekies dance It, And the geek all love it. and the cheeky-Cheeky is danced like this:
- The breikin-brokin.
- The meneito.
- The roboqueitor.
- The maiquel-guolkin.
The Problem
In this problem you have to learn to dance the cheeky-Cheeky. This dancing consists of 4 BasicSteps(As listed above) that are arranged into a participantSequence. Then this sequence can be repeated an arbitrary number of times.
For example, if the sequence is "123", then the cheeky-Cheeky is danced like this: "12312312312312... ". but if the sequence is "123124", then the steps of the dancing are "123124123124123... ".
You are given some of the steps of a particle dancing. Those steps will contain in between 2 (random) and 3 (not random) times the basic sequence. You have to continue the dancing.
For example, if the basic sequence is "123", we can have the following possibilities:
|
Input |
Output |
123123 |
12312312... |
1231231 |
23123123... |
12312312 |
31231231... |
The input
The first line of the input contains an integer indicating the number of test cases.
Each case contains some of the first steps of a dancing. it is a single line with a list of digits (1, 2, 3 or 4) with no spaces between them. it will not have more than 2000 steps. remember that the case contains the basic sequence twice, and possibly has some more steps (but not thrice ).
The output
For each test case, the output shoshould contain the 8 following steps of the dancing, followed by three dots"...".
Sample Input
61231231231231123123121231241231241231241231241212312412312412312
Sample output
12312312... 23123123... 31231231... 12312412... 31241231... 41231241...
Question: a sequence with a maximum of four characters is given. There must be duplicates before the sequence, and the next eight digits of the sequence are output.
Use KMP to find the next array, then scan from the back to find the length of the loop section, then use the loop section to model the given string, and output the remaining part.
Code:
1 #include <cstdio> 2 #include <cstring> 3 #define MAX 2002 4 using namespace std; 5 6 char s[MAX]; 7 int l,next[MAX],le; 8 9 void getnext(){10 int k,i;11 k=-1; i=0;12 memset(next,-1,sizeof(next));13 while(i<=l-1){14 if(k==-1 || s[i]==s[k]){15 k++; i++; next[i]=k;16 }else k=next[k];17 }18 }19 20 int main()21 {22 int t,r;23 //freopen("data.txt","r",stdin);24 scanf("%d",&t);25 while(t--){26 scanf("%s",s);27 l=strlen(s);28 getnext();29 for(int i=l;i>=1;i--){30 if(i%(i-next[i])==0){31 le=i-next[i]; break;32 }33 }34 r=l%le;35 for(int i=0;i<8;i++){36 putchar(s[r]);37 r=(r+1)%le;38 }39 printf("...\n");40 }41 return 0;42 }/* Ultraviolet A 11452 */