Pagini recente » Cod sursa (job #2132153) | Cod sursa (job #1407984) | Cod sursa (job #1408092) | Cod sursa (job #2761948) | Cod sursa (job #1405130)
#include <fstream>
#include <string>
#include <vector>
using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
inline void prefixFunction(vector<int>& pi, string& s)
{
int n = s.size();
pi.assign(n, 0);
for (int i = 1; i < n; ++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;
}
}
int main()
{
ios_base::sync_with_stdio(false);
int t = 0; fin >> t;
string s;
vector<int> pi;
pi.reserve(1e6 + 1);
while (t--)
{
fin >> s;
prefixFunction(pi, s);
int res = 0;
for (int i = pi.size(); i > 0; --i)
{
if (pi[i - 1] && i % (i - pi[i - 1]) == 0)
{
res = i;
break;
}
}
fout << res << '\n';
pi.clear();
}
return 0;
}