【POJ2774】Long Long Message(后缀数组)

题面

Vjudge

Description

Little cat在Byterland的首都读物理专业。这些天他收到了一条悲伤地信息:他的母亲生病了。担心买火车票花钱太多(Byterland是一个巨大的国家,因此他坐火车回家需要16小时),他决定只给母亲发短信。

Little cat的家境并不富裕,因此他经常去营业厅查看自己发短信花了多少钱。昨天营业厅的电脑坏掉了,打印出两条很长的信息。机智的little cat很快发现:

1.信息中所有的字符都是小写英文字母,没有标点和空格。

2.所有的短信都被连在了一起——第i+1条短信直接接在第i条短信后面——这就是这两条信息如此长的原因。

3.虽然他发的短信都被连在了一起,但由于电脑坏掉了,它们的左边或右边都可能会有许多冗余字符。

例如:如果短信是"motheriloveyou",电脑打印出的每条信息都可能是 "hahamotheriloveyou", "motheriloveyoureally", "motheriloveyouornot", "bbbmotheriloveyouaaa",等等。

4.因为这些乱七八糟的问题,little cat打印了两遍(所以有两条非常长的信息)。尽管原始的短信文本在两条信息中都一样,但两条信息在文本两侧的冗余字符都可能不一样。

给出这两条很长的信息,输出little cat写下的原始短信文本的最长可能长度。

背景:

在Byterland,短信按照美元/字节的单位计价。这就是little cat想要知道原始文本最长可能长度的原因。

为什么让你写一个程序?有四个原因:

1.little cat这些天忙于他的物理课程。

2.little cat不想透露他对母亲说了什么。

3.POJ是个好网站。

4.little cat想要从POJ那里挣点钱,并尝试说服他的母亲去医院

Input

两行两个由小写英文字母组成的字符串。字符串长度都不会超过100000

Output

一行一个整数,即little cat写下的原始文本的最长可能长度。

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit

yeaphowmuchiloveyoumydearmother

Sample Output

27

题解

把两个串接起来

现在问题就可以转换成

找两个后缀

求他们的最长公共前缀的最大值

这个最大值显然是一个\(height\)的值

因为是要从两个串中各选出一个串

所以要判断\(SA[i]\)和\(SA[i-1]\)是否在两个不同的串中

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 220000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
char ch[MAX];
int SA[MAX],x[MAX],y[MAX],t[MAX];
int Rank[MAX],height[MAX],a[MAX];
int n,n1,n2;
bool cmp(int i,int j,int k){return y[i]==y[j]&&y[i+k]==y[j+k];}
bool same(int i,int j){return ((i<=n1)&&(j<=n1))|((i>n1)&&(j>n1));}
void GetSA()
{
int m=30;
for(int i=1;i<=n;++i)t[x[i]=a[i]]++;
for(int i=1;i<=m;++i)t[i]+=t[i-1];
for(int i=n;i>=1;--i)SA[t[x[i]]--]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=0;i<=n;++i)y[i]=0;
for(int i=n-k+1;i<=n;++i)y[++p]=i;
for(int i=1;i<=n;++i)if(SA[i]>k)y[++p]=SA[i]-k;
for(int i=0;i<=m;++i)t[i]=0;
for(int i=1;i<=n;++i)t[x[y[i]]]++;
for(int i=1;i<=m;++i)t[i]+=t[i-1];
for(int i=n;i>=1;--i)SA[t[x[y[i]]]--]=y[i];
swap(x,y);
x[SA[1]]=p=1;
for(int i=2;i<=n;++i)x[SA[i]]=cmp(SA[i],SA[i-1],k)?p:++p;
if(p>=n)break;
m=p;
}
for(int i=1;i<=n;++i)Rank[SA[i]]=i;
for(int i=1,j=0;i<=n;++i)
{
if(j)j--;
while(a[i+j]==a[SA[Rank[i]-1]+j])++j;
height[Rank[i]]=j;
}
}
int main()
{
scanf("%s",ch+1);
n=n1=strlen(ch+1);
for(int i=1;i<=n1;++i)a[i]=ch[i]-96;
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)a[++n]=ch[i]-96;
n2=n-n1;
GetSA();
int ans=0;
for(int i=2;i<=n;++i)
if(!same(SA[i],SA[i-1]))
ans=max(ans,height[i]);
printf("%d\n",ans);
return 0;
}

【POJ2774】Long Long Message(后缀数组)的更多相关文章

  1. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  2. poj2774 Long Long Message 后缀数组求最长公共子串

    题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...

  3. POJ2774 Long Long Message [后缀数组]

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 29277   Accepted: 11 ...

  4. poj2774 Long Long Message(后缀数组or后缀自动机)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Long Long Message Time Limit: 4000MS   Me ...

  5. (HDU 5558) 2015ACM/ICPC亚洲区合肥站---Alice's Classified Message(后缀数组)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5558 Problem Description Alice wants to send a classi ...

  6. POJ 2774 Long Long Message 后缀数组

    Long Long Message   Description The little cat is majoring in physics in the capital of Byterland. A ...

  7. poj 2774 Long Long Message 后缀数组基础题

    Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 24756   Accepted: 10130 Case Time Limi ...

  8. POJ2774Long Long Message (后缀数组&后缀自动机)

    问题: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to ...

  9. poj 2774 Long Long Message 后缀数组LCP理解

    题目链接 题意:给两个长度不超过1e5的字符串,问两个字符串的连续公共子串最大长度为多少? 思路:两个字符串连接之后直接后缀数组+LCP,在height中找出max同时满足一左一右即可: #inclu ...

  10. POJ-2774-Long Long Message(后缀数组-最长公共子串)

    题意: 给定两个字符串 A 和 B,求最长公共子串. 分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大 ...

随机推荐

  1. [Python Study Notes] Basic I\O + File 操作

    列表操作 Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...

  2. Install Centrifugo and quick start

    Install Centrifugo and quick start Go is a perfect language - it gives developers an opportunity to ...

  3. centos6下从源码安装setuptools和pip

    1. 下载setuptools及pip的源码包 setuptools与pip都是python的模块 setuptools源码包: https://pypi.python.org/pypi/setupt ...

  4. ★Linux命令行操作技巧(作为服务器端)

    1.统计某个目录下总共有多少个文件(递归统计所有子目录)ls -lR|grep "^-"|wc -l

  5. 在OS X系统中php访问sftp时需要ssh2扩展的安装

    php -v brew install homebrew/php/php55-ssh2 [实现方式] <?php $connection = ssh2_connect('192.168.0.14 ...

  6. linux 下yum使用技巧

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 经常会遇上一些linux系统允许你上外网,而一些是不允许的,这时我们 ...

  7. Yii高级模板的安装

    1,如果你使用composer来安装的话,执行下边两条命令. composer global require "fxp/composer-asset-plugin:^1.2.0" ...

  8. uva1347 经典dp

    详细的思路书上面有,有一点要强调的是题意容易理解错:必须严格向右或则向左移动,不能到了第3个点又回到第2个点.否则这个状态方程是不成立的,变成了NP难问题 状态方程: dp[i][j]=min(dp[ ...

  9. Springdata mongodb 版本兼容 引起 Error [The 'cursor' option is required, except for aggregate with the explain argument

    在Spring data mongodb 中使用聚合抛出异常 mongodb版本 为 3.6 org.springframework.dao.InvalidDataAccessApiUsageExce ...

  10. react-native入坑随笔(持续更新中)

    一.问题: 解决方案:删除./node_modules/react-native/local-cli/core/__fixtures__/files/package.json,删除前记得备份. 二.在 ...