Long Long Message
Time Limit: 4000MS Memory Limit: 131072K
Total Submissions: 25889 Accepted: 10540
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:
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.”
【题意】【求两个串的最长公共字串】
**【题解】【后缀数组,将两个串连成一个,然后直接跑后缀数组,最后再查找公共子串】
[注意][坑爹的POJ不支持swap函数的数组交换]**
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 30
using namespace std;
int s[200010];
int a[200010],sa[200010],c[200010],t1[200010],t2[200010];
int len,r[200010],a1[200010],ans;
void solve()
{
int n=INF,i,k;
int *x=t1,*y=t2;
for(i=0;i<=n;++i) c[i]=0;
for(i=0;i<len;++i) c[x[i]=s[i]]++;
for(i=1;i<n;++i) c[i]+=c[i-1];
for(i=len-1;i>=0;i--) sa[--c[x[i]]]=i;
for(k=1;k<=len;k<<=1)
{
int p=0;
for(i=len-k;i<len;++i) y[p++]=i;
for(i=0;i<len;++i)
if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=0;i<=n;++i) c[i]=0;
for(i=0;i<len;++i) c[x[y[i]]]++;
for(i=1;i<n;++i) c[i]+=c[i-1];
for(i=len-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1; x[sa[0]]=0;
for(i=1;i<len;++i)
if (y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])
x[sa[i]]=p-1;
else x[sa[i]]=p++;
if(p>=len) break;
n=p;
}
}
void work()
{
int i,k=0;
for(i=0;i<len;++i) r[sa[i]]=i;
for(i=0;i<len;++i)
{
if(k) k--;
int j=sa[r[i]-1];
while(s[i+k]==s[j+k])
k++;
a1[r[i]]=k;
}
return;
}
int main()
{
int i,j,t;
char ch;
while((ch=getchar())&&ch>='a'&&ch<='z') s[len++]=ch-'a'+1;
s[len]=0; t=len-1; len++;
while((ch=getchar())&&ch>='a'&&ch<='z') s[len++]=ch-'a'+1;
s[len]=29; len++;
solve();
work();
for(i=1;i<len;++i)
if(a1[i]>ans)
{
if(sa[i-1]<t&&sa[i]>=t) ans=max(ans,a1[i]);
if(sa[i-1]>=t&&sa[i]<t) ans=max(ans,a1[i]);
}
printf("%d\n",ans);
return 0;
}
[手残调了半个上午的窝欲哭无泪……[怒摔键盘!!!]]