spoj220求至少出现两次且不重叠的最长公共子串

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=1000000+10;
int *rank,r[MAX],sa[MAX],height[MAX],n;
int wa[MAX],wb[MAX],wm[MAX],pos[15],xy[15][2];
char s[MAX/10];

bool cmp(int *r,int a,int b,int l){
	return r[a] == r[b] && r[a+l] == r[b+l];
}

void makesa(int *r,int *sa,int n,int m){
	int *x=wa,*y=wb,*t;
	for(int i=0;i<m;++i)wm[i]=0;
	for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
	for(int i=1;i<m;++i)wm[i]+=wm[i-1];
	for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
	for(int i=0,j=1,p=0;p<n;j=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<m;++i)wm[i]=0;
		for(i=0;i<n;++i)wm[x[y[i]]]++;
		for(i=1;i<m;++i)wm[i]+=wm[i-1];
		for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
		for(t=x,x=y,y=t,x[sa[0]]=0,i=p=1;i<n;++i){
			x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
		}
	}
	rank=x;
}

void calheight(int *r,int *sa,int n){
	for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k){
		for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
	}
}

void check(int k){
	int i=0;
	for(i=0;i<=n;++i)if(k<pos[i])break;
	xy[i][0]=min(xy[i][0],k);
	if(xy[i][0] == -1)xy[i][0]=k;
	xy[i][1]=max(xy[i][1],k);
}

bool panduan(int k){
	int i=0;
	for(i=0;i<n;++i)if(xy[i][1]-xy[i][0]<k)return false;
	return true;
}

int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		int len=-1,j=0;
		for(int i=0;i<n;++i){
			cin>>s;
			for(j=0,++len;s[j] != '\0';++j,++len)r[len]=s[j];
			r[len]=300+i;
			pos[i]=len;
		}
		r[len]=0;
		makesa(r,sa,len+1,310);
		calheight(r,sa,len);
		int l=0,r=len,mid;
		while(l<=r){
			mid=l+r>>1;
			memset(xy,-1,sizeof xy);
			for(int i=1;i<=len;++i){
				if(height[i]>=mid){
					check(sa[i]);
					check(sa[i-1]);
				}else{
					if(panduan(mid))break;
					memset(xy,-1,sizeof xy);
				}
			}
			if(panduan(mid))l=mid+1;
			else r=mid-1;
		}
		cout<<r<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值