https://www.cnblogs.com/widsom/p/8058358.htm (详细解释)
//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char a[maxn];
ull b[maxn];
ull c[maxn];
int main()
{
b[]=; ull d=;
for(int i=;i<=maxn;i++) b[i]=b[i-]*d;
while()
{
scanf("%s",a+); int n=strlen(a+);
if(n== && a[]=='.') break;
for(int i=;i<=n;i++) c[i]=c[i-]*d+(a[i]-'a'+);
vector<int> vs; for(int i=;i<=n;i++) { if(n%i==) vs.push_back(i); }
int ans=;
for(int i=;i<vs.size();i++)
{
int flag=;
for(int j=*vs[i];j<=n;j+=vs[i])
{
ull x=c[j]-c[j-vs[i]]*b[vs[i]];
if(x!=c[vs[i]]) {flag=; }
}
if(flag) {ans=n/vs[i]; break; }
}
printf("%d\n",ans);
}
}
//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char ss[maxn];
int nt[maxn];
int KMP()
{
int l=strlen(ss);
nt[]=-;
for (int i = ; i < l; ++i) {
int j = nt[i-];
while(ss[j+] != ss[i] && j >= ) j = nt[j];
if(ss[j+] == ss[i]) nt[i] = j+;
else nt[i] = -;
}
for(int i=;i<l;i++)
{
cout<<nt[i]<<endl;
}
int k=l-(nt[l-]+);
if(l%k==) return l/k;
else return ;
}
int main()
{ while()
{
scanf("%s",ss); int n=strlen(ss);
if(n== &&ss[]=='.') break;
printf("%d\n",KMP());
}
}
KMP
//#include<bits/stdc++.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+;
char ss[maxn];
int nt[maxn];
int KMP()
{
int l=strlen(ss); // ss
//int t=strlen(tt);
nt[]=-;
int num=;
for(int i=,j=-;i<l;)
{
if(j==-||ss[i]==ss[j])
{
nt[i]=j; // next[1]=
i++; j++; }
else
j=nt[j];
}
int k=l-(nt[l-]+);
if(l%k==) return l/k;
else return ;
}
int main()
{ while()
{
scanf("%s",ss); int n=strlen(ss);
if(n== &&ss[]=='.') break;
printf("%d\n",KMP());
}
}
KMP
桶排序
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rank rk
using namespace std; typedef long long LL; const int maxn = , M = ;
// sa[1-n] -> 0->n-1;
int sa[maxn], rank[maxn], height[maxn]; int wa[maxn], wb[maxn], wv[maxn], cnt[maxn];
void SA(int *r, int n, int m) {
int *x = wa, *y = wb;
for(int i = ; i < m; i++) cnt[i] = ;
for(int i = ; i < n; i++) cnt[x[i] = r[i]]++;
for(int i = ; i < m; i++) cnt[i] += cnt[i - ];
for(int i = n - ; i >= ; i--) sa[--cnt[x[i]]] = i;
for(int j = ; j < n; j <<= ) {
int p = ;
for(int i = n - j; i < n; i++) y[p++] = i;
for(int i = ; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(int i = ; i < n; i++) wv[i] = x[y[i]];
for(int i = ; i < m; i++) cnt[i] = ;
for(int i = ; i < n; i++) cnt[wv[i]]++;
for(int i = ; i < m; i++) cnt[i] += cnt[i - ];
for(int i = n - ; i >= ; i--) sa[--cnt[wv[i]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for(int i = ; i < n; i++)
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + j] == y[sa[i] + j] ? p - : p++;
if(p >= n) break;
m = p;
}
} void calcHeight(int *r, int n) {
int i, j, k;
for(i = j = k = ; i < n; height[rank[i++]] = k)
for(k ? k-- : , j = sa[rank[i] - ]; r[i + k] == r[j + k]; k++);
} int n, s[maxn];
char str[maxn];
int lg[maxn];
int main() {
for(int i=;i<maxn;i++){lg[i]=lg[i>>]+;}
while()
{
scanf("%s", str); n = strlen(str);
if(n== && str[]=='.') break;
for(int i = ; i < n; i++) s[i] = str[i]; s[n] = ;
SA(s, n + , M);
for(int i = ; i <= n; i++) rank[sa[i]] = i;
calcHeight(s, n);
int ans = ;
for (int i = ; i <= n; ++i) {
if(n%i == ) {
if(rank[]-rank[i] == && height[rank[]] == n-i) {
ans = n/i;
break;
}
}
}
printf("%d\n",ans);
}
return ;
}
会T
DC3