概述
REPEATS - 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: 4since a (4, 3)-repeat is found starting at the 5th character of the input string.
ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
using namespace std;
char str[53030];
int sa[53030],Rank[53030],rank2[53030],height[53030],c[53030],*x,*y,s[53030];
int n;
void cmp(int n,int sz)
{
int i;
memset(c,0,sizeof(c));
for(i=0;i<n;i++)
c[x[y[i]]]++;
for(i=1;i<sz;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[y[i]]]]=y[i];
}
void build_sa(char *s,int n,int sz)
{
x=Rank,y=rank2;
int i,j;
for(i=0;i<n;i++)
x[i]=s[i],y[i]=i;
cmp(n,sz);
int len;
for(len=1;len<n;len<<=1)
{
int yid=0;
for(i=n-len;i<n;i++)
{
y[yid++]=i;
}
for(i=0;i<n;i++)
if(sa[i]>=len)
y[yid++]=sa[i]-len;
cmp(n,sz);
swap(x,y);
x[sa[0]]=yid=0;
for(i=1;i<n;i++)
{
if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])
x[sa[i]]=yid;
else
x[sa[i]]=++yid;
}
sz=yid+1;
if(sz>=n)
break;
}
for(i=0;i<n;i++)
Rank[i]=x[i];
}
void getHeight(char *s,int n)
{
int k=0;
for(int i=0;i<n;i++)
{
if(Rank[i]==0)
continue;
k=max(0,k-1);
int j=sa[Rank[i]-1];
while(s[i+k]==s[j+k])
k++;
height[Rank[i]]=k;
}
}
int minv[53010][20],lg[53030];
void init_lg()
{
int i;
lg[1]=0;
for(i=2;i<52020;i++)
{
lg[i]=lg[i>>1]+1;
}
}
void init_RMQ(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
minv[i][0]=height[i];
}
for(j=1;j<=lg[n];j++)
{
for(k=0;k+(1<<j)-1<=n;k++)
{
minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
}
}
}
int lcp(int l,int r)
{
l=Rank[l];
r=Rank[r];
if(l>r)
swap(l,r);
l++;
int k=lg[r-l+1];
return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i,j,k;
for(i=0;i<n;i++)
{
scanf("%s",str+i);
}
build_sa(str,n+1,128);
getHeight(str,n);
init_lg();
init_RMQ(n);
int maxn=0;
for(i=1;i<n;i++)
{
for(j=0;j+i<n;j+=i)
{
int k=lcp(j,j+i);
int now=k/i;
int tj=j-(i-k%i);
if(tj>=0)
{
if(lcp(tj,tj+i)>=i-k%i)
now++;
}
if(now>maxn)
maxn=now;
}
}
printf("%dn",maxn+1);
}
}
最后
以上就是调皮秀发为你收集整理的SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)的全部内容,希望文章能够帮你解决SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复