您的位置:首页 > 其它

poj 2774 Long Long Message

2016-04-17 20:03 316 查看
Long Long Message

Time Limit: 4000MSMemory Limit: 131072K
Total Submissions: 25832Accepted: 10512
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:

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

Source

POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."

题解: 最长公共子串,后缀数组的应用。但是这里要引进height数组,用来求LCP 最长公共前缀,height[i]表示sa[i-1]和sa[i]的最长公共前缀。这道题就是利用了这个height数组,先把两个字符串用一个没有出现过的字符连接,如果相邻的两个后缀分别属于两个字符串,那么就可以用他们的height 来更新答案。

o(n)求height 数组,需要用一个辅助数组h[i]=height[rank[i]],然后按照h[1],h[2],....h
的顺序递推计算,递推的关键是性质h[i]>=h[i-1]-1.
设suffix(k)是排在suffix(i-1)前一名的后缀,则它们的最长公共前缀是h[i-1]。suffix(k)和suffix(i-1)分别删除首字符之后得到suffix(k+1)和suffix(i),那么suffix(k+1)将排在suffix(i)的前面(这里要求h[i-1]>1,如果h[i-1]≤1,原式显然成立)并且suffix(k+1)和suffix(i)的最长公共前缀是h[i-1]-1,所以suffix(i)和在它前一名的后缀的最长公共前缀至少是h[i-1]-1。

(ps:一个性质:对于两个后缀j,k,若rank[j]<rank[k],则后缀j 和k的LCP长度等于height[rank[j]+1],height[rank[j]+2]....height[rank[k]]中的最小值,即RMQ(height,rank[j]+1,rank[k])
)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200003
using namespace std;
int n,m,a
,sa[2]
,rank[2]
,v
,k,h
,p,q;
char ch
;
void calcsa(int sa
,int rank
,int SA
,int Rank
)
{
for (int i=1;i<=n;i++)
v[rank[sa[i]]]=i;
for (int i=n;i>=1;i--)
if (sa[i]>k)   SA[v[rank[sa[i]-k]]--]=sa[i]-k;
for (int i=n-k+1;i<=n;i++)
SA[v[rank[i]]--]=i;
for (int i=1;i<=n;i++)
Rank[SA[i]]=Rank[SA[i-1]]+(rank[SA[i]]!=rank[SA[i-1]]||rank[SA[i]+k]!=rank[SA[i-1]+k]);
}
void work()
{
p=0,q=1;
for (int i=1;i<=n;i++) v[a[i]]++;
for (int i=1;i<=30;i++) v[i]+=v[i-1];
for (int i=1;i<=n;i++)
sa[p][v[a[i]]--]=i;
for (int i=1;i<=n;i++)
rank[p][sa[p][i]]=rank[p][sa[p][i-1]]+(a[sa[p][i-1]]!=a[sa[p][i]]);
k=1;
while(k<n)
{
calcsa(sa[p],rank[p],sa[q],rank[q]);
p^=1; q^=1; k<<=1;
}
}
void calcheight()//LCP 最长公共前缀,h[i]表示sa[i-1]和sa[i]的最长公共前缀
{
k=0;
for (int i=1;i<=n;i++)
if (rank[p][i]==1) h[rank[p][i]]=0;
else
{
int j=sa[p][rank[p][i]-1];
while (a[i+k]==a[j+k]) k++;
h[rank[p][i]]=k;
if (k>0) k--;
}
}
int main()
{
scanf("%s",ch+1);
n=strlen(ch+1);
ch[n+1]='z'+1; n++;
m=n;
scanf("%s",ch+1+n);
n=strlen(ch+1);
for (int i=1;i<=n;i++)
a[i]=ch[i]-'a'+1;
work();
calcheight();
int ans=0;
for (int i=2;i<=n;i++)
if (sa[p][i]>m&&sa[p][i-1]<m||sa[p][i]<m&&sa[p][i-1]>m)
ans=max(h[i],ans);
printf("%d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: