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
题意:求两个字符串的最长公共子串,用dp的话是n方。我们可以把两个字符串接到一起,中间用一个没有出现的字符隔开,然后利用高度数组求一波最长公共前缀,并且判断两个后缀是不是分别来自两个串,记录一下最大值就可以了。
我看到有人在其他人的博客下问过这个问题:有没有可能是,height记录的是重叠的两个子串都在第一串上,但不符合在两个串上的条件,就排除了,而实际是有解的呢?
答案是没有可能,因为一个后缀与它相邻排名的后缀的公共前缀的长度一定大于等于与它不相邻后缀的公共前缀的长度,而在这题里找到的最大值必须要在不同的串内,按照后缀数组的排名,至少有一个在前面的串和在后面的串的排名是相邻的,这样的话就不会出现所问的问题。
#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>
using namespace std;
const int MAXN = 200010;
int SA[MAXN], rank[MAXN], Height[MAXN], tax[MAXN], tp[MAXN], a[MAXN], n, m;
char str[MAXN];
void RSort()
{
for (int i = 0; i <= m; i ++) tax[i] = 0;
for (int i = 1; i <= n; i ++) tax[rank[tp[i]]] ++;
for (int i = 1; i <= m; i ++) tax[i] += tax[i-1];
for (int i = n; i >= 1; i --) SA[tax[rank[tp[i]]] --] = tp[i];
}
int cmp(int *f, int x, int y, int w) { return f[x] == f[y] && f[x + w] == f[y + w]; }
void Suffix()
{
for (int i = 1; i <= n; i ++) rank[i] = a[i], tp[i] = i;
m = 127 ,RSort();
for (int w = 1, p = 1, i; p < n; w += w, m = p)
{
for (p = 0, i = n - w + 1; i <= n; i ++) tp[++ p] = i;
for (i = 1; i <= n; i ++) if (SA[i] > w) tp[++ p] = SA[i] - w;
RSort(), swap(rank, tp), rank[SA[1]] = p = 1;
for (i = 2; i <= n; i ++) rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++ p;
}
int j, k = 0;
for(int i = 1; i <= n; Height[rank[i ++]] = k)
for( k = k ? k - 1 : k, j = SA[rank[i] - 1]; a[i + k] == a[j + k]; ++ k);
}
void Init()
{
for(int i=1;i<=n;i++)
a[i] = str[i];
Suffix();
}
char str2[MAXN];
int main(void)
{
int i,j;
while(scanf("%s",str+1)==1)
{
scanf("%s",str2+1);
n = strlen(str+1);
int len = n;
str[++n] = '$';
for(i=1;str2[i];i++)
str[++n] = str2[i];
str[n+1] = '\0';
Init();
int maxn = 0;
for(i=2;i<=n;i++)
{
if(Height[i] > maxn)
{
if(SA[i] <= len && SA[i-1] > len || SA[i] > len && SA[i-1] <= len)
maxn = Height[i];
}
}
printf("%d\n",maxn);
}
return 0;
}