POJ2406之后缀数组

来源:互联网 发布:windows7系统优化 编辑:程序博客网 时间:2024/05/17 04:06

E - Power Strings

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算法,但是既然我是在刷SA,那么我就要用后缀数组来解决这个问题2333~~~

如果是用后缀数组来解决,就要用DC3模板,而不能用普通的倍增法(DA)构造,不然会TLE到死。=. =

然后我们求出了SA数组,height数组,rank数组。

接下来就要干活啦。

我们可以就样例ababab来说:

这里写图片描述

经过发现,我们可以找到一个规律,要有长度为i的循环节,就要满足这个条件:
rank[0]−rank[i]=1。

并且:
height[rank[0]]=len−i。

于是就有以下的代码:

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<algorithm>#define F(x) ((x)/3+((x)%3==1?0:tb))#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)using namespace std;const int maxn = int(3e6)+10;const int N = maxn;int wa[maxn],wb[maxn],wv[maxn],ws[maxn];int RANK[N],height[N],rm[N],sa[N];char a[N];int b[N];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) //涵义与DA 相同{    int i,j,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;    int *rn=r+n;    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 calheight(int *r,int *sa,int n){   // 此处N为实际长度    int i,j,k=0;    // height[]的合法范围为 1-N, 其中0是结尾加入的字符    for(i=1;i<=n;i++)        RANK[sa[i]]=i;    // 根据SA求RANK    for(i=0;i<n; height[RANK[i++]] = k )        // 定义:h[i] = height[ RANK[i] ]        for(k?k--:0,j=sa[RANK[i]-1];            r[i+k]==r[j+k]; k++);    //根据 h[i] >= h[i-1]-1 来优化计算height过程}void RMQ(int n){    int k = RANK[1];    rm[k] = N;    for(int i=k-1;i>=0;i--)    {        rm[i]=min(rm[i+1],height[i+1]);    }    for(int i=k+1;i<=n;i++)    {        rm[i]=min(rm[i-1],height[i]);    }}void solve(int le){    for(int i=1;i<=le;i++)//枚举长度    {        if(le%i!=0)//不能整除的话,一定不能构成循环节。            continue;        if(RANK[i]+1!=RANK[0])        //rank数组必须要相邻才能构成循环节。        {            continue;        }        if(height[RANK[0]]!=le-i)        //若第一个和第二个的最长公共前缀不符合条件。            continue;        printf("%d\n",le/i);        return ;    }    printf("1\n");    return ;}long long nxt[N];int main (void){    while(~scanf("%s",a))    {        if(a[0]=='.')            break;        int le=strlen(a);        for(int i=0;i<le;i++)        {            b[i]=a[i]-'a'+'0';        }        dc3(b,sa,le+1,200);        calheight(b,sa,le);        RMQ(le);        solve(le);    }    return 0;}

然后还有kmp算法的实现,可以自己去看一看,有时间我会补上来的~

0 0