Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 30427   Accepted: 12337
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."
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream> using namespace std; /****************************************后缀数组模板****************************************/
const int maxn=+;
struct SuffixArray
{
char s[maxn];
int sa[maxn];//保存着排序后的后缀
int rank[maxn];//保存着每个后缀的名词
int height[maxn];//保存着相邻前缀的最长公共子序列的长度
int t1[maxn],t2[maxn],c[maxn],n;
int dmin[maxn][];
void build_sa(int m)//m为每个后缀后面加的元素,保证在原字符串中不出现
{
int i,*x=t1,*y=t2;
for(i=;i<m;i++) c[i]=;
for(i=;i<n;i++) c[x[i]=s[i]]++;
for(i=;i<m;i++) c[i]+=c[i-];
for(i=n-;i>=;i--) sa[--c[x[i]]]=i;
for(int k=;k<=n;k<<=)
{
int p=;
for(i=n-k;i<n;i++) y[p++]=i;
for(i=;i<n;i++)if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=;i<m;i++) c[i]=;
for(i=;i<n;i++) c[x[y[i]]]++;
for(i=;i<m;i++) c[i]+=c[i-];
for(i=n-;i>=;i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p=,x[sa[]]=;
for(i=;i<n;i++)
x[sa[i]]= y[sa[i]]==y[sa[i-]]&&y[sa[i]+k]==y[sa[i-]+k]? p-:p++;
if(p>=n) break;
m=p;
}
}
void build_height()//n不能等于1,否则出BUG
{
int i,j,k=;
for(i=;i<n;i++)rank[sa[i]]=i;
for(i=;i<n;i++)
{
if(k)k--;
j=sa[rank[i]-];
while(s[i+k]==s[j+k])k++;
height[rank[i]]=k;
}
}
void initMin()
{
for(int i=;i<=n;i++) dmin[i][]=height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dmin[i][j]=min(dmin[i][j-] , dmin[i+(<<(j-))][j-]);
}
int RMQ(int L,int R)//取得范围最小值
{
int k=;
while((<<(k+))<=R-L+)k++;
return min(dmin[L][k] , dmin[R-(<<k)+][k]);
}
int LCP(int i,int j)//求后缀i和j的LCP最长公共前缀
{
int L=rank[i],R=rank[j];
if(L>R) swap(L,R);
L++;//注意这里
return RMQ(L,R);
}
}sa;
/****************************************后缀数组模板****************************************/ char s[maxn*+]; int main(int argc, char *argv[])
{
// freopen("in.txt","r",stdin);
scanf("%s",sa.s);
int n=strlen(sa.s);
sa.s[n]=;
scanf("%s",sa.s+n+);
sa.n=strlen(sa.s);
sa.build_sa();
sa.build_height();
int _max=sa.height[];
for(int i=;i<sa.n+;i++){
int a=sa.sa[i],b=sa.sa[i-];
if(a>b) swap(a,b);
if( a<n&&b>n ){//属于两个字符串
_max=max(_max,sa.height[i]);
}
}
printf("%d\n",_max);
return ;
}

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

  1. (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 ...

  2. POJ 2774 Long Long Message 后缀数组

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 2774 Long Long Message 后缀数组模板题

    题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...

  10. POJ 2774 Long Long Message (后缀数组+二分)

    题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在 ...

随机推荐

  1. oracle11g 体系结构详解

    1.oracle内存由SGA+PGA所构成 2.oracle数据库体系结构数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制. oracle工作原理: 1).在数据库 ...

  2. 微信小程序-发送模板消息(C#)

    步骤一:获取模板ID 有两个方法可以获取模版ID 通过模版消息管理接口获取模版ID 在微信公众平台手动配置获取模版ID 步骤二:页面的 <form/> 组件,属性report-submit ...

  3. 【OOM】GC overhead limit exceeded

    我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...

  4. HTML 简述

    1.本页超链接 <a href="#t">查看 Chapter 4.</a> <a name="t">Test</a& ...

  5. 17.tslib安装以及使用

    1.先在网上下载 tslib-1.4.tar.gz压缩包 2.然后在ubuntu编译: tar xzf tslib-1.4.tar.gz cd tslib ./autogen.sh mkdir tmp ...

  6. bzoj4033(树上染色)

    树上染色 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...

  7. JavaScript 实现发布消息后,距离当前时间的实现

    某条消息发布后,距离当前时间多久的时间显示 //显示发布时间的函数 function pastTime(_createTime) { //var createTime = _createTime.su ...

  8. POJ 2472 106 miles to Chicago(Dijstra变形——史上最坑的最长路问题)

    题目链接 :http://poj.org/problem?id=2472 Description In the movie "Blues Brothers", the orphan ...

  9. Docker镜像加速

    安装好Docker后接下来就是愉快的玩耍了,可是实际情况并不愉快 因为docker的默认镜像源在国外,鉴于国内特殊的网络原因访问起来往往很慢或者完全访问不了,幸好国内有类似的镜像源可以使用,我使用的是 ...

  10. Java+Velocity模板引擎集成插件到Eclipse及使用例子

    一.因为我用的是当前最新的Eclipse4.5,Eclipse中安装集成VelocityEclipse插件之前需要先安装其支持插件:Eclipse 2.0 Style Plugin Support 1 ...