UVa - 11452 - Dancing the Cheeky-Cheeky
先上题目:
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 basic steps (as listed above) that are arranged into a particular sequence. 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 particular dancing. Those steps will contain between 2 (inclusive) and 3 (not inclusive) 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 should contain the 8 following steps of the dancing, followed by three dots "...
".
Sample Input
6 123123 1231231 12312312 123124123124 12312412312412 12312412312412312
Sample Output
12312312... 23123123... 31231231... 12312412... 31241231... 41231241...
题意:给出最多只有4种字符的序列,这个序列前面一定有重复的部分,将这个序列接下来的八位输出。
用kmp求一次next数组,然后从后往前扫找到循环节的长度,然后就用循环节模一下给出的串,将剩下的部分输出就可以了。
上代码:
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 }