Pagini recente » Cod sursa (job #2285490) | Cod sursa (job #1646459) | Cod sursa (job #35221) | Cod sursa (job #1306241) | Cod sursa (job #1653218)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
char S[1000100];
int p[1000100];
int T,rs;
int main(){
ios::sync_with_stdio(0);
fin.tie(0);
(fin >> T).ignore(1);
while(T--){
fin.getline(S+1,1000100);
int i,k,l,n;
l = k = rs = n = p[1] = 0;
for(i = 2;S[i];i++){
while(k && S[k+1] != S[i])
k = p[k];
if(S[k+1] == S[i]) k++;
p[i] = k;
if(k && i-k == l) n++;
else
if(k && i-k > l) l = i-k, n = 1;
else n = 0;
if(n && n >= l) rs = l*(n/l+1);
//fout << S[i] << ' ' << k << ' ' << l << ' ' << n << ' ' << rs << '\n';
}
fout << rs << "\n";
}
}