poj 2774

Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 36765   Accepted: 14741
Case Time Limit: 1000MS

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

Source 

 

题意:给你两串字符,要你找出在这两串字符中都出现过的最长子串.........

解法一: 

我做了二分+hash,然后由于子串的数量太多,冲突的可能性很高,我又加了一个特判,居然就过了。  

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<set>  
 7 using namespace std;  
 8 int const N=100000+3 ;  
 9 int const mod=1e9+7;  
10 int const p=10000759;   
11 int const sum=200003;  
12 char s1[N],s2[N];
13 int pw[N],h1[N],h2[N];     
14 int a[N<<1],b[N<<1],w; 
15 int find(int x){
16     int k=x% sum;  
17     while (a[k]!=x && b[k] ) k=(k+1)% sum;  
18     w=k;  
19     if(b[k]) return -1;  
20     else return k; 
21 }
22 int pd(int x,int y,int mid){
23     for(int i=0;i<mid;i++)  
24         if(s1[x+i]!=s2[y+i]) return 0; 
25     return 1; 
26 }
27 int check(int mid) { 
28     memset(b,0,sizeof(b));   
29     memset(a,-1,sizeof(a));     
30     int len1=strlen(s1);  
31     for(int i=mid;i<=len1;i++){
32         int k=(1LL*h1[i]-1LL*h1[i-mid]*pw[mid]) % mod;  
33         k=(k+mod)% mod;  
34         int t=find(k);  
35         if(t>-1) a[t]=k,b[t]=i-mid+1;   
36     } 
37     int len2=strlen(s2);  
38     for(int i=mid; i<=len2;i++) {
39         int k=(1LL*h2[i]-1LL*h2[i-mid]*pw[mid]) % mod; 
40         k=(k+mod)% mod; 
41         int t=find(k); 
42         if(t==-1) {
43             if(pd(b[w]-1,i-mid,mid)) return 1; 
44         }
45     }
46     return 0; 
47 }
48 int main(){
49     scanf("%s",s1);  
50     scanf("%s",s2);  
51     pw[0]=1;  
52     for(int i=1;i<=100000;i++) 
53         pw[i]=1LL*pw[i-1]*p% mod;  
54     for(int i=0;s1[i];i++)    
55         h1[i+1]=(1LL*h1[i]*p+s1[i]) % mod;  
56     for(int i=0;s2[i];i++)  
57         h2[i+1]=(1LL*h2[i]*p+s2[i]) % mod;  
58     int l=0,r=max(strlen(s1),strlen(s2))+1;
59     while (l<r-1){
60         int mid=(l+r)/2;  
61         if(check(mid)) l=mid;  
62         else r=mid;  
63     }
64     cout<<l<<endl; 
65     return 0; 
66 }
View Code

 解法二: 

利用后缀数组h数组的性质,我们把两个字符串连接起来,中间加一个特殊的字符,然后求出sa,最后的答案就是在h数组里面排名相邻的两个前缀,如果一个属于第一个字符串,另一个属于第2个字符串,那么就可能是我们的答案,用h[i]更新ans

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm> 
 6 using namespace std;  
 7 int const N=200000+3;  
 8 int sa[N],rk[N],wa[N<<1],wb[N<<1],num[N],wv[N],n,m,h[N];  
 9 char s1[N<<1],s2[N];
10 int cmp(int *r,int x,int y,int k){
11     return r[x+k]==r[y+k] && r[x]==r[y];  
12 } 
13 void build_sa(char *r,int *sa,int n,int m){
14     int i,j,*x=wa,*y=wb,p;
15     for(i=0;i<m;i++) num[i]=0;  
16     for(i=0;i<n;i++) num[x[i]=r[i]]++;  
17     for(i=1;i<m;i++) num[i]+=num[i-1];          
18     for(i=n-1;i>=0;i--)  sa[--num[x[i]]]=i;  
19     for(p=1,j=1;p<n;j<<=1,m=p){
20         for(p=0,i=n-j;i<n;i++) y[p++]=i;  
21         for(i=0;i<n;i++) if(sa[i]>=j)  y[p++]=sa[i]-j;  
22         for(i=0;i<m;i++) num[i]=0;  
23         for(i=0;i<n;i++) wv[i]=x[y[i]];      
24         for(i=0;i<n;i++) num[wv[i]]++;  
25         for(i=1;i<m;i++) num[i]+=num[i-1];  
26         for(i=n-1;i>=0;i--)  sa[--num[wv[i]]]=y[i]; 
27         swap(x,y); 
28         for(p=1,x[sa[0]]=0,i=1; i<n;i++) 
29             x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ?  p-1 : p++;  
30     }
31     for(i=0;i<n;i++) rk[i]=x[i];   
32 }
33 void build_h(char *r,int *sa,int n){
34     int k=0; 
35     for(int i=0;i<n;i++){    
36         if(k) k--;  
37         int j=sa[rk[i]-1];  
38         while (r[i+k]==r[j+k]) k++;  
39         h[rk[i]]=k;   
40     }
41 }
42         
43 int main(){
44     scanf("%s",s1); 
45     scanf("%s",s2);  
46     int l1=strlen(s1);  
47     s1[l1]=125;  
48     strcat(s1,s2);  
49     n=strlen(s1);  
50     build_sa(s1,sa,n+1,130);  
51     build_h(s1,sa,n);  
52     n=l1;  
53     m=strlen(s2); 
54     int ans=0; 
55     for(int i=2;i<=n+m+1;i++) 
56         if(sa[i]<n &&  sa[i-1]>n || sa[i]>n &&  sa[i-1]<n) 
57             ans=max(ans,h[i]);  
58     printf("%d\n",ans); 
59     return 0; 
60 }
View Code

 

posted @ 2019-05-30 14:58  zjxxcn  阅读(144)  评论(0编辑  收藏  举报