poj2406Power Strings(DC3)

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 38455   Accepted: 15966

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

首先,这一题的正解应该是KMP,然而用后缀数组也是能做的,但是有卡nlogn
通过这一题再一次明确了几个地方
1.刘汝佳白书上的后缀数组求height[]是错的
2.输入一个字符串,应该在其末尾补上一个0
3.rank1[0~n-1]都是大于等于1的
4.通过这道题目也初步了解了如果用RMQ维护所有点到固定一点的最大LCS
5. suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k。(k表示为长度)如果知道这一点便可以说明这个字符串可以由若干个循环节构成
#include<stdio.h>
#include<cstring>
#include <cstdlib>
#include<algorithm>
using namespace std;
const int maxn=3000010;
int ws[maxn],wa[maxn],wb[maxn],wv[maxn],sa[maxn],rank1[maxn],height[maxn],a[maxn],f[maxn];
char s[maxn];
//dc3
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int c0(int *r,int a,int b)
{
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b)
{
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0; i<n; i++) wv[i]=r[a[i]];
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[wv[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) b[--ws[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m)
{
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0; i<tbc; i++) san[rn[i]]=i;
    for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0; i<ta && j<tbc; p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(; i<ta; p++) sa[p]=wa[i++];
    for(; j<tbc; p++) sa[p]=wb[j++];
    return;
}

void getHeight(int n){
    int i,j,k=0;
    for(i=0;i<=n;i++)    rank1[sa[i]]=i;
    for(i=0;i<n;i++){
        if(k) k--;
        int j=sa[rank1[i]-1];
        while(s[i+k]==s[j+k])   k++;
        height[rank1[i]]=k;
    }
}

void st(int n){
    int j=rank1[0];
    int minv=n;
    for(int i=j;i>=1;i--){  //rank1值,自己和自己的长度为n
        f[i]=minv;          //排名为0的f[0]始终为0
        minv=min(minv,height[i]);
    }
    minv=n;
    for(int i=j+1;i<=n;i++){
        minv=min(minv,height[i]);
        f[i]=minv;
    }
}

int main(){
    while(scanf("%s",s)!=EOF){
        if(s[0]=='.')
            break;
        int n=strlen(s);
        for (int i=0; i<n; i++) a[i]=static_cast<int>(s[i]);
        a[n]=0;
        dc3(a,sa,n+1,123);
        getHeight(n);
        st(n);
        /*for(int i=0;i<n;i++){   //rank1[0~n-1]对应于1~n
            printf("rank[%d] is %d\n",i,rank1[i]);
            printf("%d\n",height[rank1[i]]);
        }*/
        int ans;
        for(int i=1;i<=n;i++){      //表示长度,等于n是保证结果为1的情况
            if(n%i==0&&f[rank1[i]]==n-i){    //不从0开始是因为f[rank1[0]]=n,不可能成立
                ans=n/i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(poj2406Power Strings(DC3))