Description
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string
s = abaabaabaaba
is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.
Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string
u = babbabaabaabaabab
contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.
Input
In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.
Output
For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.
Example
Input:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b
Output:
4
since a (4, 3)-repeat is found starting at the 5th character of the input string.
题意:给我们一串字符,需要求这串字符中的最长重复子串,输出长度。要是有多个,输出字典序最小的。
看到一个图解很不错的:
就是枚举多少个字符会匹配,然后求出它们的height值,再用这个值去除以长度,得到有多少个循环。
假设一个长度为l的子串重复出现两次,那么它必然会包含s[0]、s[l]、s[l*2]…之中的相邻的两个。不难看出,该重复子串必然会包含s[0..l]或s[l..l*2]或s[l*2..l*3]…。所以,我们可以枚举一个i,对于每个i*l的位置,利用后缀数组可以求出s[i*l..(i+1)*l]向后延伸的长度k。k/l+1即i*l..(i+1)*l这一段重复出现的次数。但还有一种情况。考虑以下的字符串:
aababababab
如果长度下还有多余的那么我要不全一个长度l,那么我就向左移(l-k%l)个单位 即:i-(l-k%l) 那么在判断左边是否有可以匹配的
假设现在l=2,i=1。则当前得到的子串为ba.用后缀数组可以求得k=7,则ba共重复出现了4次。但实际上,长度为2的子串重复出现最多的应该是“ab”,出现了5次。可以看出来,上述方法求得的k不能整除l,故可能在i的左边位置存在一个子串能完整重复覆盖i这个子串后面的子串。这里是i左边一位的“ab”子串。分析下这种情况,可以得知,如果以i-(l-k%l)开头的长度为l的子串,向后延伸的长度能大于k的话,那么有一个子串出现次数为k/l+2。
附上代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = int(1e6)+10;
int wa[MAXN],wb[MAXN],wv[MAXN],WS[MAXN];
int RANK[MAXN],height[MAXN];
int mm[MAXN];
int best[20][MAXN];
int rm[MAXN];
char a[MAXN];
int t1[MAXN],t2[MAXN],c[MAXN];
int r[MAXN];
int b[MAXN],sa[MAXN];
int cmp(int *r,int a,int b,int l)
{
return (r[a]==r[b]) && (r[a+l]==r[b+l]);
}
void DA(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) WS[i]=0;
for(i=0;i<n;i++) WS[x[i]=r[i]]++;
for(i=1;i<m;i++) WS[i]+=WS[i-1];
for(i=n-1;i>=0;i--) sa[--WS[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[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--) sa[--WS[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
//printf("p = %d\n", p );
}
}
void calheight(int *r,int *sa,int n)
{
// memset(height,0,sizeof(height));
// memset(RANK,0,sizeof(RANK));
int i,j,k=0;
for(i=1;i<=n;i++) RANK[sa[i]]=i;
for(i=0;i<n; height[RANK[i++]] = k )
for(k?k--:0,j=sa[RANK[i]-1]; r[i+k]==r[j+k]; k++);
}
void initheight(int n)
{
mm[0]=-1;
for(int i=1;i<=n;i++)
mm[i]=( (i&(i -1)) ==0)? mm[i- 1]+1: mm[i- 1];
for(int i=1;i<=n;i++)
best[0][i]=i;
for(int i=1;i<=mm[n];i++)
for(int j=1;j+(1<<i)-1<=n;j++)
{
int a=best[i-1][j];
int b=best[i-1][j+(1<<(i-1))];
if(height[a]<height[b])
best[i][j]=a;
else best[i][j]=b;
}
}
int askheight(int a,int b)
{
int t;
t=mm[b-a+1];
b-=(1<<t)-1;
a=best[t][a];
b=best[t][b];
return height[a]<height[b]?a:b;
}
int lcp(int a,int b)
{
a=RANK[a];
b=RANK[b];
if(a>b) swap(a,b);
return height[askheight(a+1,b)];
}
int main (void)
{
int t;
scanf("%d",&t);
while(t--)
{
int num;
scanf("%d",&num);
for(int i=0;i<num;i++)
scanf(" %c",&a[i]);
int le=num;
if(num==1)
{
printf("1\n");
continue;
}
//int n = 2*le+1;
for(int i=0;i<le;i++)
{
b[i]=a[i]-'a'+1;
}
b[le]=0;
DA(b,sa,le+1,128);
calheight(b,sa,le);
initheight(le);
int maxx=0;
int t,r=0,k;
for(int l=1;l<le;l++)
{
for(int i=0;i+l<le;i+=l)//枚举
{
k=lcp(i,i+l);
r=k/l+1;//要加上自己
t=i-(l-(k%l));
if(t>=0&&k%l!=0)
{
int tmp=lcp(t,t+l);
if(tmp/l+1>r)
r=tmp/l+1;
}
maxx=max(maxx,r);
}
}
printf("%d\n",maxx);
}
return 0;
}