Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 23696   Accepted: 9705
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

field=source&key=POJ+Monthly--2006.03.26">POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."

ac代码

/*Problem: 2774  User: kxh1995
Memory: 5024K Time: 891MS
Language: C++ Result: Accepted */
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b? b:a)
using namespace std;
char str1[200100],str2[2000100];
int sa[220020],c[220020],t2[220020];
int t1[220020],s[200100];
int rank[220020],height[220020];
int len1,len2;
void build_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++)
c[i]=0;
for(i=0;i<n;i++)
c[x[i]=s[i]]++;
for(i=1;i<m;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[i]]]=i;
for(j=1;j<=n;j<<=1)
{
p=0;
for(i=n-j;i<n;i++)
y[p++]=i;
for(i=0;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=0;i<m;i++)
c[i]=0;
for(i=0;i<n;i++)
c[x[y[i]]]++;
for(i=1;i<m;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;
x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)
break;
m=p;
}
}
void getHeight(int s[],int n)
{
int i,j,k=0;
for(i=0;i<=n;i++)
rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)
k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k])
k++;
height[rank[i]]=k;
}
}
int judge(int len,int k)
{
int i;
for(i=1;i<len;i++)
{
if(height[i]>=k)
{
if(sa[i]>len1&&sa[i-1]<=len1)
return 1;
if(sa[i-1]>len1&&sa[i]<=len1)
return 1;
}
}
return 0;
}
int main()
{
while(scanf("%s%s",str1,str2)!=EOF)
{
int i;
len1=strlen(str1);
len2=strlen(str2);
for(i=0;i<len1;i++)
{
s[i]=str1[i]-'a'+1;
}
s[len1]=27;
int n=len1+1;
for(i=0;i<len2;i++)
s[n++]=str2[i]-'a'+1;
s[n]=0;
build_sa(s,len1+len2+3,28);
getHeight(s,len1+len2+2);
int l=0,r=min(len1,len2),ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(judge(len1+len2+2,mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
}

POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)的更多相关文章

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

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

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

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 19206   Accepted: 79 ...

  3. poj2774 后缀数组 求最长公共子串

    Reference:IOI2009论文 http://www.cnblogs.com/ziyi--caolu/p/3192731.html #include "stdio.h" # ...

  4. poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14874   Accepted: 5118 De ...

  5. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

  6. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

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

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

  8. poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403

    题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...

  9. POJ 2774 Long Long Message 后缀数组

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

随机推荐

  1. poi判断一行是隐藏的getZeroHeight()

    poi判断一行是隐藏的 getZeroHeight() boolean isZeroHeight = row.getZeroHeight(); if(isZeroHeight){ // 如果为隐藏行就 ...

  2. Auto property synthesis will not synthesize property &#39;delegate&#39;; it will be implemented by its super

    今天在XCode6.3上面重写TabBar的时候,自己定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件里 警告消失

  3. Ubuntu输入su提示认证失败的解决方法

    用su切换,输入密码提示认证失败,这下搞了吧,后来一经查阅原来Ubuntu安装后,root用户默认是被锁定了的,不允许登录,也不允许 su 到 root ,对于桌面用户来说这个可能是为了增强安全性,但 ...

  4. Oracle新建Schema

    1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...

  5. invalidate

    转载请注明出处:                前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时真挚地向渴望了解      A ...

  6. ES内存持续上升问题定位

      https://discuss.elastic.co/t/memory-usage-of-the-machine-with-es-is-continuously-increasing/23537/ ...

  7. 【POJ 1741】 Tree

    [题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...

  8. 杂项:MSP(管理服务提供商)

    ylbtech-杂项:MSP(管理服务提供商) 随着外包市场的日益成熟,为了满足企业的需求,一个全新的业务方向被开发出来—MSP.MSP采用业界领先的系统管理技术,由经验丰富的系统管理专家通过WAN为 ...

  9. js写发布微博文本框---2017-04-14

    实现效果: 1.文本框输入内容,低端字数对应减少 2.当文本框内容超出时,会显示字数超出多少 效果图如下: 实现代码: <!DOCTYPE html><html> <he ...

  10. 动态连通性问题:union-find算法

    写在前面的话: 一枚自学Java和算法的工科妹子. 算法学习书目:算法(第四版) Robert Sedgewick 算法视频教程:Coursera  Algorithms Part1&2 本文 ...