Repeats SPOJ - 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.

真的是太难了,菜狗只能抄了

 RMQ:http://blog.csdn.net/qq_31759205/article/details/75008659

  |

 LCP:

还是大牛写的好啊:http://blog.csdn.net/u013480600/article/details/23974119

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef long long ll;
const int maxn=50010;
char str[maxn];
int s[maxn],t1[maxn],t2[maxn],c[maxn];
int sa[maxn],height[maxn],rank[maxn];

void get_sa(int n,int m)
{
	int i,j,*x=t1,*y=t2,p;
	Rep(i,m) c[i]=0;
	Rep(i,n) c[x[i]=s[i]]++;
	Rep(i,m) c[i+1]+=c[i];
	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;
		Rep(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;

		Rep(i,m) c[i]=0;
		Rep(i,n) c[x[y[i]]]++;
		Rep(i,m) c[i+1]+=c[i];
		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]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
		if(p>=n) break;
		m=p;
	}
}

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

int d[maxn][20];
void init_rmq(int n){// 注意height的特点 
	for(int i=1;i<=n;i++) d[i][0]=height[i];
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
		}
	}
}
int RMQ(int L,int R){
	int k=0;
	while((1<<(k+1))<=R-L+1) k++;
	return min(d[L][k],d[R-(1<<k)+1][k]);
}

int LCP(int i,int j){
	int L=rank[i],R=rank[j];
	if(L>R) swap(L,R);
	L++;//height的特点 
	return RMQ(L,R);
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i=0; i<n; i++)
			scanf(" %c",&str[i]);
		for(int i=0; i<n; i++)
			s[i]=str[i];
		s[n]=0;
		get_sa(n+1,128);
		get_height(n);
		init_rmq(n);
	
		int ans=1;
		for(int len=1; len<n; len++) {
			for(int j=0; j+len<n; j+=len) {
				int k=LCP(j,j+len);
				int now=k/len+1;
				int jj=j-(len-k%len);
				if(jj>=0) {
					if(LCP(jj,jj+len)/len+1>now)
						now++;
				}
				ans=max(ans,now);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef long long ll;
const int maxn=50010;
char str[maxn];
int s[maxn],t1[maxn],t2[maxn],c[maxn];
int sa[maxn],height[maxn],rank[maxn];

void get_sa(int n,int m)
{
	int i,j,*x=t1,*y=t2,p;
	Rep(i,m) c[i]=0;
	Rep(i,n) c[x[i]=s[i]]++;
	Rep(i,m) c[i+1]+=c[i];
	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;
		Rep(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;

		Rep(i,m) c[i]=0;
		Rep(i,n) c[x[y[i]]]++;
		Rep(i,m) c[i+1]+=c[i];
		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]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
		if(p>=n) break;
		m=p;
	}
}

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

int mm[maxn],dmin[maxn][20];
void initMin(int n){
	mm[0]=-1;
	for(int i=1;i<=n;i++){
		dmin[i][0]=height[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++)
			dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);
	}
}

int RMQ(int L,int R){
	int k=mm[R-L+1];
	return min(dmin[L][k],dmin[R-(1<<k)+1][k]);
}

int LCP(int i,int j){
	int L=rank[i],R=rank[j];
	if(L>R) swap(L,R);
	L++;
	return RMQ(L,R);
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i=0; i<n; i++)
			scanf(" %c",&str[i]);
		for(int i=0; i<n; i++)
			s[i]=str[i];
		s[n]=0;
		get_sa(n+1,128);
		get_height(n);
		initMin(n);
	
		int ans=1;
		for(int len=1; len<n; len++) {
			for(int j=0; j+len<n; j+=len) {
				int k=LCP(j,j+len);
				int now=k/len+1;
				int jj=j-(len-k%len);
				if(jj>=0) {
					if(LCP(jj,jj+len)/len+1>now)
						now++;
				}
				ans=max(ans,now);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值