[poj2774] Long Long Message
Posted hihocoder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[poj2774] Long Long Message相关的知识,希望对你有一定的参考价值。
[poj2774] 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:
- The little cat is so busy these days with physics lessons;
- The little cat wants to keep what he said to his mother seceret;
- POJ is such a great Online Judge;
- 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."
题解
题目给定两个字符串(A)和(B),求最长公共子串的长度
后缀数组中不是有一个(Height[])数组求最长公共前缀吗?于是我们就考虑用后缀数组求解
我们将(A)和(B)连成一个字符串,中间加一个其它权值的字符,后面再加一个最小权值,这样我们求出来的不会有(A)后(+B)前的为公共前缀
答案就是排序后相邻的两个后缀,它们满足一个首字符在(A)部分,另一个首字符在(B)部分,它们的(Height[])的最大值
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=250000;
int l,n,sa[N],rk[N],c[N],tmp[N],height[N];
char s[N];
void Read()
{
cin>>s;
l=strlen(s),s[l]=2;
cin>>(s+l+1);
n=strlen(s),s[n++]=1;
}
void Make_Sa()
{
int i,j,k,len,na;
na=256;
memset(c,0,na*sizeof(int));
for(i=0;i<n;++i)
{
rk[i]=s[i]&0xff,
++c[rk[i]];
}
for(i=1;i<na;++i) c[i]+=c[i-1];
for(i=0;i<n;++i)
{
--c[rk[i]],
sa[c[rk[i]]]=i;
}
for(len=1;len<n;len<<=1)
{
for(i=0;i<n;++i)
{
j=sa[i]-len;
if(j<0) j+=n;
tmp[c[rk[j]]++]=j;
}
c[0]=0,
sa[tmp[0]]=0,
j=0;
for(int i=1;i<n;++i)
{
if(rk[tmp[i]]!=rk[tmp[i-1]]||
rk[tmp[i]+len]!=rk[tmp[i-1]+len])
c[++j]=i;
sa[tmp[i]]=j;
}
memcpy(rk,sa,n*sizeof(int)),
memcpy(sa,tmp,n*sizeof(int));
if(j>=n-1) break;
}
}
void Lcp()
{
int i=0,j,k=0;
height[0]=0;
for(j=rk[0];i<n-1;++i,++k)
for(;k>=0&&s[i]!=s[sa[j-1]+k];)
{
height[j]=k, --k,
j=rk[sa[j]+1];
}
}
void PutAns()//核心部分
{
int Ans=0;
for(int i=1;i<n;++i)
if((sa[i]>l&&sa[i-1]<l)||(sa[i]<l&&sa[i-1]>l))
if(height[i]>Ans) Ans=height[i];
printf("%d
",Ans);
}
int main()
{
Read(),Make_Sa(),Lcp(),PutAns();
return 0;
}
以上是关于[poj2774] Long Long Message的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2774 Long Long Message(后缀数组[最长公共子串])
POJ 2774 Long Long Message 后缀数组
POJ 2774 Long Long Message(后缀数组[最长公共子串])