poj 2774 Long Long Message(后缀数组)

本文深入探讨了一段短信服务中的数学问题,揭示了短信服务中心打印出的长消息背后的奥秘。通过解决这个问题,小猫不仅巧妙地避免了昂贵的长途电话费用,还巧妙地表达了对母亲的爱意。文章详细介绍了如何通过数学方法找到最原始的短信文本长度,展现了数学在日常生活中的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Long Long Message
Time Limit: 4000MS Memory Limit: 131072K
Total Submissions: 25832 Accepted: 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[n]的顺序递推计算,递推的关键是性质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[N],sa[2][N],rank[2][N],v[N],k,h[N],p,q;
char ch[N];
void calcsa(int sa[N],int rank[N],int SA[N],int Rank[N])
{
	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);
} 

2016.12.28

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 200003
using namespace std;
int n,m,len,a[N],b[N],xx[N],yy[N],*x,*y;
int sa[N],rank[N],height[N],p;
char s[N];
int cmp(int i,int j,int l)
{
	return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
void get_SA()
{
	x=xx; y=yy; m=30;
	for (int i=1;i<=len;i++) b[x[i]=a[i]]++;
	for (int i=1;i<=m;i++) b[i]+=b[i-1];
	for (int i=len;i>=1;i--) sa[b[x[i]]--]=i;
	for (int k=1;k<=len;k<<=1){
		p=0;
		for (int i=len-k+1;i<=len;i++) y[++p]=i;
		for (int i=1;i<=len;i++)
		 if (sa[i]>k) y[++p]=sa[i]-k;
		for (int i=1;i<=m;i++) b[i]=0;
		for (int i=1;i<=len;i++) b[x[y[i]]]++;
		for (int i=1;i<=m;i++) b[i]+=b[i-1];
		for (int i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
		swap(x,y); p=2; x[sa[1]]=1;
		for (int i=2;i<=len;i++)
		 x[sa[i]]=cmp(sa[i],sa[i-1],k)?p-1:p++;
		if (p>len) break;
		m=p+1;
	}
	p=0;
	for (int i=1;i<=len;i++) rank[sa[i]]=i;
	for (int i=1;i<=len;i++) {
		if (rank[i]==1) continue;
		int j=sa[rank[i]-1];
		while (i+p<=len&&j+p<=len&&a[i+p]==a[j+p]) p++;
		height[rank[i]]=p;
		p=max(p-1,0);
    }
}
int main()
{
    freopen("a.in","r",stdin);
	scanf("%s",s+1);
    n=strlen(s+1);
    for (int i=1;i<=n;i++) a[++len]=s[i]-'a'+1;
    a[++len]=29; int m1=len;
    scanf("%s",s+1);
    n=strlen(s+1);
    for (int i=1;i<=n;i++) a[++len]=s[i]-'a'+1;
    get_SA();
    int ans=0;
    for (int i=2;i<=len;i++) 
     if (sa[i]>m1&&sa[i-1]<m1||sa[i]<m1&&sa[i-1]>m1) 
	  ans=max(ans,height[i]);
    printf("%d\n",ans);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值