/*
https://www.luogu.com.cn/problem/UVA455
最小周期: n - pi[n -1]
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> prefix_fun(string s)
{
int len = s.length();
//pi[i]: 子串s[0...i]最长的相等的真前缀与真后缀的长度
vector<int> pi(len);
for (int i = 1; i < len; ++i)
{
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
++j;
pi[i] = j;
}
return pi;
}
int main()
{
int num;
cin >> num;
string str;
for (int i = 0; i < num; ++i)
{
cin >> str;
vector<int> pi = prefix_fun(str);
cout << str.length() - pi[str.length() - 1] << endl;
}
return 0;
}
vector<int> find_occurrences(string text, string pattern) {
string cur = pattern + '#' + text;
int sz1 = text.size(), sz2 = pattern.size();
vector<int> v;
vector<int> lps = prefix_fun(cur);
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
if (lps[i] == sz2)
v.push_back(i - 2 * sz2);
}
return v;
}
前缀函数与 KMP 算法 - OI Wiki