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+10;
char a[maxn];
ull b[maxn];
ull c[maxn];
int main()
{
    b[0]=1; ull d=33;
    for(int i=1;i<=maxn;i++) b[i]=b[i-1]*d;
    while(1)
    {
        scanf("%s",a+1); int n=strlen(a+1);
        if(n==1 && a[1]=='.') break;
        for(int i=1;i<=n;i++) c[i]=c[i-1]*d+(a[i]-'a'+1);
        vector<int> vs; for(int i=1;i<=n;i++)  { if(n%i==0) vs.push_back(i); }
        int ans=1;
        for(int i=0;i<vs.size();i++)
        {
            int flag=1;
            for(int j=2*vs[i];j<=n;j+=vs[i])
            {
                ull x=c[j]-c[j-vs[i]]*b[vs[i]];
                if(x!=c[vs[i]]) {flag=0; }
            }
            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+10;
char ss[maxn];
int nt[maxn];
int KMP()
{
    int l=strlen(ss);
    nt[0]=-1;
    for (int i = 1; i < l; ++i) {
        int j = nt[i-1];
        while(ss[j+1] != ss[i] && j >= 0) j = nt[j];
        if(ss[j+1] == ss[i]) nt[i] = j+1;
        else nt[i] = -1;
    }
    for(int i=0;i<l;i++)
    {
        cout<<nt[i]<<endl;
    }
    int k=l-(nt[l-1]+1);
    if(l%k==0) return l/k;
    else return 1;
}
int main()
{

    while(1)
    {
        scanf("%s",ss); int n=strlen(ss);
        if(n==1 &&ss[0]=='.') 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+10;
char ss[maxn];
int nt[maxn];
int KMP()
{
    int l=strlen(ss);  // ss
    //int t=strlen(tt);
    nt[0]=-1;
    int num=0;
    for(int i=0,j=-1;i<l;)
    {
        if(j==-1||ss[i]==ss[j])
        {
            nt[i]=j; // next[1]=
            i++; j++;

        }
        else
            j=nt[j];
    }
    int k=l-(nt[l-1]+1);
    if(l%k==0) return l/k;
    else return 1;
}
int main()
{

    while(1)
    {
        scanf("%s",ss); int n=strlen(ss);
        if(n==1 &&ss[0]=='.') 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 = 1050005, M = 300;
// 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 = 0; i < m; i++) cnt[i] = 0;
    for(int i = 0; i < n; i++) cnt[x[i] = r[i]]++;
    for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
    for(int i = n - 1; i >= 0; i--) sa[--cnt[x[i]]] = i;
    for(int j = 1; j < n; j <<= 1) {
        int p = 0;
        for(int i = n - j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(int i = 0; i < n; i++) wv[i] = x[y[i]];
        for(int i = 0; i < m; i++) cnt[i] = 0;
        for(int i = 0; i < n; i++) cnt[wv[i]]++;
        for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
        for(int i = n - 1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i];
        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(int i = 1; i < n; i++)
            x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
}

void calcHeight(int *r, int n) {
    int i, j, k;
    for(i = j = k = 0; i < n; height[rank[i++]] = k)
        for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
}

int n, s[maxn];
char str[maxn];
int lg[maxn];
int main() {
    for(int i=2;i<maxn;i++){lg[i]=lg[i>>1]+1;}
    while(1)
    {
        scanf("%s", str); n = strlen(str);
        if(n==1 && str[0]=='.') break;
        for(int i = 0; i < n; i++) s[i] = str[i]; s[n] = 0;
        SA(s, n + 1, M);
        for(int i = 0; i <= n; i++) rank[sa[i]] = i;
        calcHeight(s, n);
        int ans = 1;
        for (int i = 1; i <= n; ++i) {
            if(n%i == 0) {
                if(rank[0]-rank[i] == 1 && height[rank[0]] == n-i) {
                    ans = n/i;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
会T

DC3