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
题意: 求两字符串的最长公共子串长度。
分析: 将第二个字符串接在第一个字符串后面跑一遍后缀数组,取height数组的最大值,这也就是任意两后缀的最长公共前缀最大值,但这时候可能取到的最大长度来自于同一个字符串,于是我们要取sa[i]和sa[i-1]分别来自两个字符串的height[i]最大值,为什么要取相邻两个后缀呢?假设答案为len,答案后缀为i和j且sa[i]和sa[j]并不相邻,则len = min(height[sa[i]+1], ..., height[sa[j]]),由于答案为len且为其中的最小值,可以知道其中至少有一个位置pos的height等于len,而从sa[i]+1到pos与pos-1到sa[j]这两段区间一定有一个是分别在字符串两侧的,也就是答案区间可以被进一步缩小,以此类推直到答案是在相邻两后缀上取得。同时为了防止匹配越界需要在两字符串中间加上一个不会用到的字符'#',时间复杂度为O(n),n是字符总数。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;
const int maxn = 1e6+10;
int n, m, k;
char s[maxn];
int sa[maxn], height[maxn], x[maxn], y[maxn], rk[maxn], tong[maxn];
int len1;//第一个字符串长度
void get_sa()
{
for(int i = 1; i <= n; i++) tong[x[i] = s[i]] ++;
for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
for(int i = n; i; i--) sa[tong[x[i]]--] = i;
for(int k = 1; k <= n; k <<= 1)
{
int num = 0;
for(int i = n-k+1; i <= n; i++) y[++num] = i;
for(int i = 1; i <= n; i++)
{
if(sa[i] <= k) continue;
y[++num] = sa[i] - k;
}
for(int i = 0; i <= m; i++) tong[i] = 0;
for(int i = 1; i <= n; i++) tong[x[i]]++;
for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
for(int i = n; i; i--) sa[tong[x[y[i]]]--] = y[i], y[i] = 0;
for(int i = 0; i <= 2*num; i++)
{
int temp = x[i];
x[i] = y[i];
y[i] = temp;
}
x[sa[1]] = 1, num = 1;
for(int i = 2; i <= n; i++)
x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i] + k] == y[sa[i-1] + k]) == 1 ? num : ++ num;
if(n == num) return;
m = num;
}
}
void get_height()
{
for(int i = 1; i <= n; i++) rk[sa[i]] = i;
for(int i = 1, k = 0; i <= n; i++)
{
if(rk[i] == 1) continue;
if(k) k--;
int j = sa[rk[i]-1];
while(i + k <= n && j + k <= n && s[i+k] == s[j+k]) k++;
height[rk[i]] = k;
}
}
void solve()
{
scanf("%s", s+1);
len1 = strlen(s+1);
s[len1+1] = '#';//加入不可能出现的分隔符
scanf("%s", s+len1+2);
n = strlen(s+1);
get_sa();
get_height();
int ans = 0;
for(int i = 1; i <= n; i++)
if(height[i] > ans && min(sa[i], sa[i-1]) < len1 && max(sa[i], sa[i-1]) > len1+1)
ans = height[i];
cout << ans;
}
signed main()
{
m = 200;//每次都需要初始化!
for(int i = 0; i <= m; i++)
tong[i] = 0;
solve();
return 0;
}