spoj 220 Relevant Phrases of Annihilation

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

(in the example above, the longest substring which fulfills the requirements is 'ba')

算法分析:
做法和上题大同小异,也是先将 n 个字符串连起来,中间用不相同的且没有
出现在字符串中的字符隔开,求后缀数组。然后二分答案,再将后缀分组。判断
的时候,要看是否有一组后缀在每个原来的字符串中至少出现两次,并且在每个
原来的字符串中,后缀的起始位置的最大值与最小值之差是否不小于当前答案
(判断能否做到不重叠,如果题目中没有不重叠的要求,那么不用做此判断)。
这个做法的时间复杂度为 O(nlogn)。

 1 #include<bits/stdc++.h>
 2 using namespace std;  
 3 int const N=200000+1000; 
 4 int const inf=10000000;   
 5 int wa[N<<1],wb[N<<1],wv[N],num[N],rk[N],h[N],sa[N],r[N],id[N],vx[11],vy[11],n;    
 6 char s1[N]; 
 7 int cmp(int *r,int x,int y,int z){
 8     return r[x]==r[y] && r[x+z]==r[y+z];  
 9 }  
10 void build_sa(int *r,int *sa,int n,int m){
11     int i,j,p,*x=wa,*y=wb;  
12     for(i=0;i<m;i++) num[i]=0;  
13     for(i=0;i<n;i++) num[x[i]=r[i]]++;  
14     for(i=1;i<m;i++) num[i]+=num[i-1];  
15     for(i=n-1;i>=0;i--)  sa[--num[x[i]]]=i;  
16     for(j=1,p=1;p<n;j<<=1,m=p){
17         for(p=0,i=n-j;i<n;i++) y[p++]=i;  
18         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;  
19         for(i=0;i<m;i++) num[i]=0;  
20         for(i=0;i<n;i++) num[wv[i]=x[y[i]]]++;  
21         for(i=1;i<m;i++) num[i]+=num[i-1];  
22         for(i=n-1;i>=0;i--) sa[--num[wv[i]]]=y[i];  
23         swap(x,y);  
24         for(i=1,p=1,x[sa[0]]=0;i<n;i++)  
25             x[sa[i]]=cmp(y,sa[i],sa[i-1],j)? p-1:p++; 
26     }
27     for(i=0;i<n;i++)  rk[i]=x[i]; 
28 }  
29 void build_h(int *r,int *sa,int n){
30     int k=0; 
31     for(int i=0;i<n;i++){
32         if(k) k--;  
33         int j=sa[rk[i]-1];  
34         while (r[i+k]==r[j+k]) k++;  
35         h[rk[i]]=k;  
36     }
37 } 
38 int check(int mid,int len ){ 
39     for(int i=1;i<=n;i++)  vx[i]=inf,vy[i]=-1;    
40     for(int i=1;i<=len;i++){
41         if(h[i]>=mid){
42             int a=id[sa[i]];  
43             int b=id[sa[i-1]];  
44             vx[a]=min(vx[a],sa[i]);  
45             vx[b]=min(vx[b],sa[i-1]);  
46             vy[a]=max(vy[a],sa[i]);  
47             vy[b]=max(vy[b],sa[i-1]); 
48             int check=0;  
49             for(int j=1;j<=n;j++){
50                 if(vy[j]-vx[j]<mid) {
51                     check=1;  break;  
52                 }
53             }
54             if(!check)  return  1; 
55         }else {
56             for(int j=1;j<=n;j++) vx[j]=inf,vy[j]=-1;    
57         }
58     }
59     return 0; 
60 }
61 int main(){
62     int cas;  
63     scanf("%d",&cas);  
64     while (cas--){
65         memset(r,0,sizeof(r)); 
66         memset(id,0,sizeof(id));      
67         int sum=122;  
68         scanf("%d",&n);
69         int len=0;  
70         for(int j=1;j<=n;j++) {
71             scanf("%s",s1);   
72             for(int i=0;s1[i];i++)  
73                 r[len++]=s1[i],id[len-1]=j;  
74             r[len++]=++sum;  
75         } 
76         build_sa(r,sa,len+1,140);  
77         build_h(r,sa,len);  
78         int l=0,r=len,ans=0;  
79         while (l<=r){
80             int mid=(l+r)/2;  
81             if(check(mid,len)) {
82                 ans=mid;  
83                 l=mid+1; 
84             }else r=mid-1;  
85         } 
86         printf("%d\n",ans);       
87     }
88     return 0; 
89 }
View Code

猜你喜欢

转载自www.cnblogs.com/ZJXXCN/p/11003720.html