题目链接:https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/pattern-3/submissions/code/1301226664
题意:求字符串的最小循环节。
解法:KMP裸题,思想在训练指南P211-P214
//Hacker Rank Pattern 3
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int f[maxn];
string s;
int main(){
int t;
scanf("%d", &t);
while(t--){
cin >> s;
int n = s.size();
memset(f,0,sizeof(f)); //根据其前一个字母得到
for(int i=1;i<s.size();i++)
{
int j=f[i];
while(j && s[i]!=s[j])
j=f[j];
f[i+1]=(s[i]==s[j])?j+1:0;
}
printf("%d\n", n - f[n]);
}
return 0;
}