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

原创 2013年12月03日 22:00:02

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 4289 最大流最小割

点击打开链接 题意:给定的点中,每个点都有自己的权值,问从s走到d,删除几个点之后走不到了,且这几个点的权值最小 思路:看着就像是最小割,直接模版敲完,因为权值在点上,而不是边上,所以要将点拆开,...
  • Dan__ge
  • Dan__ge
  • 2016年04月05日 15:49
  • 727

BZOJ2095: [Poi2010]Bridges

题目大意:给一张图,每条边有一个a到b的代价,同时还有一个b到a的代价,求一个经过所有边所有点的路径使得路径代价最大值最小 首先知道要二分,这样这张图就变成了一些边固定了方向而另一些边两个方...
  • commonc
  • commonc
  • 2016年09月05日 19:43
  • 188

SPOJ 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 题目:给定n个字符串,求出...

spoj 220. Relevant Phrases of Annihilation( 后缀数组 每个字符串至少出现两次且不重叠的最长子串)

给定 n 个字符串,求在每个字符串中至少出现两次且不重叠的最长子串。

SPOJ220.Relevant Phrases of Annihilation 给出n个字符串,求一个最长的串x,x在每个字符串中不重叠出现至少两次

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

spoj 220 Relevant Phrases of Annihilation (后缀数组 每个串中都至少出现两次的不重叠最长子串)

题意:给出10个长度为10^4的串,求在每个串中都至少出现两次的不重叠子串最大长度...

SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串

Description You are the King of Byteland. Your agents have just intercepted a batch of encrypte...

SPOJ 1812 Longest Common Substring II 后缀自动机求多字符串最长公共子串

题意:给若干字符串,求它们的最长公共子串的长度。题解:后缀自动机。对第一个串建立SAM,并拓扑排序。 用后面的串分别匹配。 对于SAM,每个节点新增两个值ml,ans; ans代表该节点满足所有...
  • cdsszjj
  • cdsszjj
  • 2017年07月25日 19:01
  • 197

SPOJ1812(后缀自动机求n个串的最长公共子串)

题目:http://www.spoj.com/problems/LCS2/   题意:给定n个串,求它们的最长公共子串。   思路就是:先将一个串建SAM,然后用后面的串去匹配,对于每一个串,...

HDU 3518 Boring counting(后缀数组啊 求字符串中不重叠的重复出现至少两次的子串的个数)

HDU 3518 Boring counting(后缀数组啊 求字符串中不重叠的重复出现至少两次的子串的个数)...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:spoj220求至少出现两次且不重叠的最长公共子串
举报原因:
原因补充:

(最多只允许输入30个字)