D

题目大意:

     给一个t,接下来每个t,给一个串,求出最小循环节下还要增加多少个珠子才完美。(要是都没啥循环节,就输出长度)

解题思路:

    求出最小循环节 cir:cir=len - next[len] (关于为什么是这个式子详见《KMP 专题知识》),然后拿len%cir得到的余数就是已经有的,那么拿循环节再减掉有的,就是需要增加的。即:cir-len%cir。如果len%cir == 0,而且cir!=len,就意味着一颗都不用增加,因为此时已经是完美的循环了,但是要是cir==len,就说明没有所谓的循环节,就要加len颗。

参考代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <map>
 4 #include <string>
 5 #include <queue>
 6 #include <stack>
 7 #include <set>
 8 #include <algorithm>
 9 
10 #include <cstdio>
11 #include <cstring>
12 #include <cmath>
13 #include <cstdlib>
14 using namespace std;
15 
16 const int INF=0x3f3f3f3f;
17 const int SIZE=10000;
18 typedef long long LL;
19 
20 char b[100005];
21 int nextt[100005];
22 void nexxt()
23 {
24     memset(nextt,0,sizeof(nextt));
25     int j=0,k=-1;
26     nextt[0]=-1;
27     int len=strlen(b);
28     while(j<len)
29     {
30         if(k==-1||b[j]==b[k])
31         {
32             k++;
33             j++;
34             nextt[j]=k;
35         }
36         else
37             k=nextt[k];
38     }
39 }
40 
41 int main()
42 {
43     int t;
44     scanf("%d",&t);
45     getchar();
46     while(t--)
47     {
48          gets(b);
49          int len=strlen(b);
50          nexxt();
51          int cir=len-nextt[len];
52          if(len%cir==0&&cir!=len)
53             printf("0
");
54          else
55              printf("%d
",cir-nextt[len]%cir); 
56 
57     }
58     return 0;
59 }
View Code
まだまだだね
原文地址:https://www.cnblogs.com/xxQ-1999/p/7522541.html