首页 > 代码库 > POJ 2774 Long Long Message
POJ 2774 Long Long Message
Long Long Message
Time Limit: 4000ms
Memory Limit: 131072KB
This problem will be judged on PKU. Original ID: 277464-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 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
yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample Output
27
Source
POJ Monthly--2006.03.26
解题:求最长公共子串。
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 }
POJ 2774 Long Long Message
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。