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

猜你喜欢

转载自www.cnblogs.com/ZJXXCN/p/10949536.html
今日推荐