Long long messagetime limit: 4000 msmemory limit: 131072 kbthis problem will be judged on PKU. Original ID: 2774
64-bit integer Io format: % LLD Java class name: Main 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 Keys 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, wocould possibly be one of "messages", "motheriloveyoureally", "motheriloveyouornot", "messages ", 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 wocould 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 cocould 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 :(
Inputtwo strings with lowercase letters on two of the input lines individually. the number of characters in each one will never exceed 100000. outputa single line with a single integer number-what is the maximum length of the original text written by the little cat. sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample output
27
Sourcepoj monthly -- 2006.03.26 problem solving: Find the longest public substring.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210000;18 int n,k,_rank[maxn],sa[maxn],lcp[maxn];19 char str[maxn];20 bool cmp_sa(int i,int j) {21 if(_rank[i] != _rank[j]) return _rank[i] < _rank[j];22 int ri = i + k < n ? _rank[i+k]:0;23 int rj = j + k < n ? _rank[j+k]:0;24 return ri < rj;25 }26 void construct_sa(char *s) {27 n = strlen(s);28 memset(sa,0,sizeof(sa));29 for(int i = 0; i < n; i++) {30 sa[i] = i;31 _rank[i] = s[i];32 }33 for(k = 1; k < n; k <<= 1) {34 sort(sa,sa+n,cmp_sa);35 lcp[sa[0]] = 0;36 for(int i = 1; i < n; i++)37 lcp[sa[i]] = lcp[sa[i-1]] + cmp_sa(sa[i-1],sa[i]);38 for(int i = 0; i < n; i++)39 _rank[i] = lcp[i];40 }41 }42 void construct_lcp(char *s) {43 memset(lcp,0,sizeof(lcp));44 for(int i = 0,h = 0; i < n; i++) {45 if(h) h--;46 for(int j = sa[_rank[i]+1]; i+h < n && j + h < n && s[i+h] == s[j+h]; h++);47 lcp[_rank[i]+1] = h;48 }49 }50 int main() {51 int len,ans;52 gets(str);53 len = strlen(str);54 str[len] = ‘+‘;55 gets(str+len+1);56 construct_sa(str);57 construct_lcp(str);58 ans = 0;59 for(int i = 1; i < n; i++)60 if((sa[i] < len) != (sa[i-1] < len)) ans = max(ans,lcp[i]);61 printf("%d\n",ans);62 return 0;63 }View code
Poj 2774 long message