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:
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 :(
Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
A single line with a single integer number – what is the maximum length of the original text written by the little cat.
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
27
POJ Monthly–2006.03.26,Zeyuan Zhu,”Dedicate to my great beloved mother.”
学不动后缀自动机和后缀树的我来搞后缀数组了QAQ
虽然敲的倍增,但实在不会写双关键字排序的计数排序QAQ看网上的代码好蛋疼
连起来然后取lcp最大值就行了,由于用的sort所以复杂度 O(nlog2n) 。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int SZ = 1000010;
int n,lcp[SZ],l,sa[SZ],rank[SZ],k = 1,tmp[SZ];
bool cmp_sa(int i,int j)
{
if(rank[i] != rank[j]) return rank[i] < rank[j];
else
{
int x = i + k <= l ? rank[i + k] : -1;
int y = j + k <= l ? rank[j + k] : -1;
return x < y;
}
}
void getsa(char s[])
{
l = strlen(s);
for(int i = 0;i <= l;i ++)
{
sa[i] = i;
rank[i] = i == l ? -1 : s[i];
}
for(k = 1;k <= l;k <<= 1)
{
sort(sa,sa + 1 + l,cmp_sa);
tmp[sa[0]] = 0;
for(int i = 1;i <= l;i ++)
tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0);
for(int i = 0;i <= l;i ++)
rank[i] = tmp[i];
}
int h = 0;
for(int i = 0;i <= l;i ++)
rank[sa[i]] = i;
lcp[0] = 0;
for(int i = 0;i < l;i ++)
{
int j = sa[rank[i] - 1];
if(h) h --;
while(i + h < l && j + h < l)
{
if(s[i + h] == s[j + h]) h ++;
else break;
}
lcp[rank[i] - 1] = h;
}
}
int ask()
{
int ans = 0;
for(int i = 0;i < l;i ++)
{
// if((sa[i] < n && sa[i + 1] > n) || (sa[i] > n && sa[i + 1] < n))
if((sa[i] < n) != (sa[i + 1] < n))
ans = max(ans,lcp[i]);
}
return ans;
}
char s[SZ],s1[SZ];
int main()
{
scanf("%s",s);
s[n = strlen(s)] = '$';
scanf("%s",s1);
int m = strlen(s1);
for(int i = 0;i < m;i ++)
s[i + n + 1] = s1[i];
// printf("%s\n",s);
getsa(s);
// for(int i = 1;i <= l;i ++)
// printf("%d ",sa[i]); puts("");
printf("%d\n",ask());
return 0;
}
/* yeshowmuchiloveyoumydearmotherreallyicannotbelieveit yeaphowmuchiloveyoumydearmother */