Long Long Message(后缀数组,求最长公共子串)

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:

All characters in messages are lowercase Latin letters, without punctuations and spaces.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.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.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.”

求最长公共子串等价于求两个字符串的后缀的最长公共前缀,把2个串合起来然后中间用一个特殊字符隔开,,求出height数组,然后找出从第一个串开始的后缀和从第二个串开始的后缀的LCP,更新最大值

/*************************************************************************> File Name: POJ2774.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: 2015年03月25日 星期三 21时44分41秒 ************************************************************************/;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;LL;typedef pair <int, int> PLL;class SuffixArray{public:N = 220000;int init[N];int X[N];int Y[N];int Rank[N];int sa[N];int height[N];int buc[N];int LOG[N];int dp[N][20];int size;//初始化个数void clear(){size = 0;}void insert(int n){init[size++] = n;}bool cmp(int *r, int a, int b, int l){return (r[a] == r[b] && r[a + l] == r[b + l]);}void getsa(int m = 256){init[size] = 0;int l, p, *x = X, *y = Y, n = size + 1;for (int i = 0; i < m; ++i){buc[i] = 0;}for (int i = 0; i < n; ++i){++(buc[x[i] = init[i]]);}for (int i = 1; i < m; ++i){buc[i] += buc[i – 1];}for (int i = n – 1; i >= 0; –i){sa[–buc[x[i]]] = i;}for (l = 1, p = 1; p < n; m = p, l *= 2){p = 0;for (int i = n – l; i < n; ++i){y[p++] = i;}for (int i = 0; i < n; ++i){if (sa[i] >= l){y[p++] = sa[i] – l;}}for (int i = 0; i < m; ++i){buc[i] = 0;}for (int i = 0; i < n; ++i){++buc[x[y[i]]];}for (int i = 1; i < m; ++i){buc[i] += buc[i – 1];}for (int i = n – 1; i >= 0; –i){sa[–buc[x[y[i]]]] = y[i];}int i;for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i){x[sa[i]] = cmp(y, sa[i – 1], sa[i], l) ? p – 1 : p++;}}}void getheight(){int h = 0, n = size;for (int i = 0; i <= n; ++i){Rank[sa[i]] = i;}height[0] = 0;for (int i = 0; i < n; ++i){if (h > 0){–h;}int j = sa[Rank[i] – 1];for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);height[Rank[i]] = h;}}//预处理每一个数字的对数,用于rmq,常数优化void initLOG(){LOG[0] = -1;for (int i = 1; i < N; ++i){LOG[i] = (i & (i – 1)) ? LOG[i – 1] : LOG[i – 1] + 1;}}void initRMQ(){initLOG();int n = size;int limit;for (int i = 0; i < n; ++i){dp[i][0] = height[i];}for (int j = 1; j <= LOG[n]; ++j){limit = (n – (1 << j));for (int i = 0; i <= limit; ++i){dp[i][j] = min(dp[i][j – 1], dp[i + (1 << j) – 1][j – 1]);}}}int LCP(int a, int b){int t;a = Rank[a];b = Rank[b];if (a > b){swap(a, b);}–b;t = LOG[b – a + 1];return min(dp[a][t], dp[b – (1 << t) + 1][t]);}void solve(int n, int m){int ans = 0;for (int i = 1; i < size; ++i){if (i == m){continue;}if (sa[i] < m && sa[i – 1] > m && sa[i – 1] < n){ans = max(ans, height[i]);}else if (sa[i] > m && sa[i – 1] < m && sa[i] < n){ans = max(ans, height[i]);}}printf(“%d\n”, ans);}}SA;char s1[100010], s2[100010];int main(){while (~scanf(“%s%s”, s1, s2)){SA.clear();int len1 = strlen(s1);int len2 = strlen(s2);for (int i = 0; i < len1; ++i){SA.insert((int)s1[i]);}SA.insert((int)’$’);for (int i = 0; i < len2; ++i){SA.insert((int)s2[i]);}SA.getsa(256);SA.getheight();SA.solve(len1 + len2, len1);}return 0;}

将会错过更好的风景,保持一份平和,保持一份清醒。

Long Long Message(后缀数组,求最长公共子串)

相关文章:

你感兴趣的文章:

标签云: