Repeats
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.
题目链接:https://vjudge.net/problem/SPOJ-REPEATS
题目大意:给一个长度为n的字符串,求这个字符串重复次数最多的连续重复子串的重复次数
思路:罗穗骞的论文《后缀数组——处理字符串的有力工具》中写的很详细,就不再赘述。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=500005;
int a[N],id[N],sa[N],rak[N],h[N],c[N],t1[N],t2[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w];}
void da(int a[], int sa[], int rak[], int h[], int n, int m)
{
n++;
int i,j,p,*x = t1,*y = t2;
for (i=0; i<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[i]=a[i]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
for (j=1; j<=n; j<<=1)
{
p=0;
for (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<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[y[i]]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
swap(x, y); p=1; x[sa[0]]=0;
for (i=1; i<n; i++) x[sa[i]]=cmp(y, sa[i-1], sa[i], j)?p-1:p++;
if(p>=n) break;
m=p;
}
n--;
for(i=0;i<=n;i++) rak[sa[i]]=i;
int k=0;
for(int i=0;i<n;h[rak[i++]]=k)
for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];k++);
}
int mm[N],dp[N][30];
void init_RMQ(int n)
{
mm[0]=-1;
for(int i=1; i<=n; i++)
{
dp[i][0]=h[i];
mm[i]=((i&(i-1))==0)? mm[i-1]+1:mm[i-1];
}
for(int j=1; j<=mm[n]; j++)
for(int i=1; i+(1<<j)-1<=n; i++)
dp[i][j]=min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}
int RMQ(int L, int R)
{
int k = mm[R-L+1];
return min(dp[L][k], dp[R-(1<<k)+1][k]);
}
int LCP(int i, int j)
{
i=rak[i],j=rak[j];
if(i>j) swap(i,j);
return RMQ(i+1,j);
}
int main()
{
int T,n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
char s;
for(int i=0;i<n;i++)
{scanf(" %c",&s);a[i]=s;}
a[n]=0;
da(a,sa,rak,h,n,128);
init_RMQ(n);
int ans=1;
for(int i=1;i<n;i++)
for(int j=0;j+i<n;j+=i)
{
int len=LCP(j,j+i);
int tp=len/i+1;
int pos=j-(i-len%i);
if(pos>=0)
{ len=LCP(pos,pos+i);
tp=max(tp,len/i+1);
}
ans=max(ans, tp);
}
printf("%d\n", ans);
}
return 0;
}