POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774
Long Long Message
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 36438 | Accepted: 14614 | |
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."
题意概括:
找两个串的最长公共子串长度
解题思路:
合并两个串 判断 height[ i ] 的 sa[ i ] 和 sa[ i -1 ] 所代表的公共子串部分 是否分别在两个不同的串中
AC code:
1 #include <set> 2 #include <map> 3 #include <cmath> 4 #include <vector> 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #define INF 0x3f3f3f3f 11 #define LL long long 12 using namespace std; 13 const int MAXN = 2e5+10; 14 //const int M = 1e6+10; 15 int M; 16 int r[MAXN]; 17 int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN]; 18 int sa[MAXN]; //index range 1~n value range 0~n-1 19 int cmp(int *r, int a, int b, int l) 20 { 21 return r[a] == r[b] && r[a + l] == r[b + l]; 22 } 23 24 void da(int *r, int *sa, int n, int m) 25 { 26 int i, j, p, *x = wa, *y = wb, *ws = tmp; 27 for (i = 0; i < m; i++) ws[i] = 0; 28 for (i = 0; i < n; i++) ws[x[i] = r[i]]++; 29 for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 30 for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; 31 for (j = 1, p = 1; p < n; j *= 2, m = p) 32 { 33 for (p = 0, i = n - j; i < n; i++) y[p++] = i; 34 for (i = 0; i < n; i++) 35 if (sa[i] >= j) y[p++] = sa[i] - j; 36 for (i = 0; i < n; i++) wv[i] = x[y[i]]; 37 for (i = 0; i < m; i++) ws[i] = 0; 38 for (i = 0; i < n; i++) ws[wv[i]]++; 39 for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 40 for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; 41 for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) 42 x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; 43 } 44 } 45 46 int Rank[MAXN]; //index range 0~n-1 value range 1~n 47 int height[MAXN]; //index from 1 (height[1] = 0) 48 void calheight(int *r, int *sa, int n) 49 { 50 int i, j, k = 0; 51 for (i = 1; i <= n; ++i) Rank[sa[i]] = i; 52 for (i = 0; i < n; height[Rank[i++]] = k) 53 for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; ++k); 54 return; 55 } 56 57 string str, tp; 58 59 int main() 60 { 61 cin >> str; 62 cin >> tp; 63 int mid = str.size(); 64 str+=tp; 65 int len = str.size(); 66 for(int i = 0; i < len; i++){ 67 r[i] = str[i]-'a'+1; 68 } 69 r[len] = 0; 70 da(r, sa, len+1, 30); 71 calheight(r, sa, len); 72 73 int ans = 0; 74 for(int i = 2; i < len; i++){ 75 if(sa[i] >= mid && sa[i-1]+height[i] < mid){ 76 ans = max(ans, height[i]); 77 } 78 else if(sa[i-1] >= mid && sa[i]+height[i] < mid){ 79 ans = max(ans, height[i]); 80 } 81 } 82 printf("%d\n", ans); 83 84 return 0; 85 }