标签:any and his htm sizeof 一个 HERE cas ges
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:
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."
题目给定两个字符串\(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\n",Ans);
}
int main()
{
Read(),Make_Sa(),Lcp(),PutAns();
return 0;
}
标签:any and his htm sizeof 一个 HERE cas ges
原文地址:https://www.cnblogs.com/hihocoder/p/12209012.html