【HDU-3746】解题报告(KMP,循环节)

原始题目

Cyclic Nacklace

  • Time Limit: 2000/1000 MS (Java/Others)
  • Memory Limit: 32768/32768 K (Java/Others)
  • Total Submission(s): 14437
  • Accepted Submission(s): 6022

Problem Description

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl's fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls' lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet's cycle is 9 and its cyclic count is 2:

1.jpg
1.jpg

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.

CC is satisfied with his ideas and ask you for help.

Input

The first line of the input is a single integer \(T ( 0 < T \le 100 )\) which means the number of test cases.

Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by 'a' ~'z' characters. The length of the string \(Len\): \(( 3 \le Len \le 100000 )\).

Output

For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.

Sample Input

3
aaa
abca
abcde

Sample Output

0
2
5

Author

possessor WC

Source

HDU 3rd “Vegetable-Birds Cup” Programming Open Contest

Recommend

lcy

题目大意

有一个手环,由不同珠子构成,若要让他成为一个“魅力手环”需要使手环由大于等于两个,完全相同的首尾相接的子串构成。 问需要补充多少珠子使手环变为魅力手环。

解题思路

  • 求循环节以及判断是否完全循环,求差值

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos((double)-1)
#define E exp(double(1))
#define K 1000000+9
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define ms(x,a) memset((x),(a),sizeof(a))
const int maxn=1e6+5;
int nt[maxn];

char a[maxn],b[maxn];
int n;
//参数为模板串和next数组
//字符串均从下标0开始
void kmpGetNext(char *s,int *Next)
{
Next[0]=0;
int len=strlen(s);
for(int i=1,j=0;i<len;i++)
{
while(j&&s[i]!=s[j]) j=Next[j];
if(s[i]==s[j]) j++;
Next[i+1]=j;
}
// Next[len]=0;
}
int kmp(char *ss,char *s,int *Next)
{
kmpGetNext(s,Next);
// 调试输出Next数组
// int len=strlen(s);
// for(int i=0;i<=n;i++)
// cout<<Next[i]<<" ";
// cout<<endl;

// int ans=0;
// int len1=strlen(ss);
// int len2=strlen(s);
for(int i=0,j=0;i<2*n;i++) //倍长
{
while(j&&ss[i%n]!=s[j])j=Next[j];
if(ss[i%n]==s[j]) j++;
if(j==n){
return 1;
}

}
return 0;
}

int main(void)
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",a);
memset(nt,0,sizeof(nt));
n=strlen(a);
kmpGetNext(a,nt);
int ans;
int k = n-nt[n];//循环节大小
if(k == n) //不循环,补充相同一串
ans = n;
else if(n % k == 0) //完全循环,不需要补充
ans = 0;
else //补充剩下的
ans = k - (n % k);
printf("%d\n",ans);


// cout<<endl;
}
}

收获与反思

  • 对于KMP循环节的问题
    • \(n-nt[n]\)求得最长循环节的大小(可能是自身)
    • \(n%(n-nt[n])\)求剩下的尾巴大小(可用来判断是不是完全循环)