转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Long Long Message
Time Limit: 4000MS | Memory Limit: 131072K | |
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 :(
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
POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."
题意:
求两个字符串的最长公共子串。
分析:
将两个字符串中间用一个不会出现的'$'符号连接,然后求出lcp,最大的且相邻的两个后缀不属于同一个字符串的就是答案。
用的是DC3

1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 #define MAXN 400010 36 37 #define F(x) ((x)/3+((x)%3==1?0:tb)) 38 #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) 39 int wa[MAXN*2],wb[MAXN*2],wv[MAXN*2],ww[MAXN*2]; 40 41 int c0(int *r, int a, int b) { 42 return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2]; 43 } 44 int c12(int k, int *r, int a, int b) 45 { 46 if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1); 47 else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1]; 48 } 49 void rsort(int *r, int *a, int *b, int n, int m) { 50 REP(i,n) wv[i]=r[a[i]]; 51 REP(i,m) ww[i]=0; 52 REP(i,n) ww[wv[i]]++; 53 REP(i,m-1) ww[i+1]+=ww[i]; 54 DEP(i,n-1,0) b[--ww[wv[i]]]=a[i]; 55 } 56 57 void dc3(int *r, int *sa, int n, int m) { 58 int j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p; 59 r[n]=r[n+1]=0; 60 REP(i,n) if(i%3!=0) wa[tbc++]=i; 61 rsort(r+2,wa,wb,tbc,m); 62 rsort(r+1,wb,wa,tbc,m); 63 rsort(r,wa,wb,tbc,m); 64 for(p=1,rn[F(wb[0])]=0,j=1;j<tbc;j++) 65 rn[F(wb[j])]=c0(r,wb[j-1],wb[j])?p-1:p++; 66 if(p<tbc) dc3(rn,san,tbc,p); 67 else REP(i,tbc) san[rn[i]]=i; 68 REP(i,tbc) if(san[i]<tb) wb[ta++]=san[i]*3; 69 if(n%3==1) wb[ta++]=n-1; 70 rsort(r,wb,wa,ta,m); 71 REP(i,tbc) wv[wb[i]=G(san[i])]=i; 72 int i; 73 for(i=j=p=0;i<ta&&j<tbc;p++) 74 sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++]; 75 for(;i<ta;p++) sa[p]=wa[i++]; 76 for(;j<tbc;p++) sa[p]=wb[j++]; 77 } 78 79 int ra[MAXN*2], height[MAXN*2]; 80 void calheight(int *r,int *sa,int n) { 81 int i,j,k=0; 82 for(i=1;i<=n;i++) ra[sa[i]]=i; 83 for(i=0;i<n;height[ra[i++]]=k) 84 for(k?k--:0,j=sa[ra[i]-1];r[i+k]==r[j+k];k++); 85 } 86 int sa[MAXN *2]; 87 char str[MAXN]; 88 char s[MAXN]; 89 int a[MAXN]; 90 int main() 91 { 92 ios::sync_with_stdio(false); 93 while(scanf("%s",str)!=EOF){ 94 scanf("%s",s); 95 int len2=strlen(s); 96 int len1=strlen(str); 97 for(int i=0;i<len2;i++){ 98 str[i+len1]=s[i]; 99 } 100 str[len1+len2]='