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

给你两串字符,要你找出在这两串字符中都出现过的最长子串
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
// KMP TRIE DFA SUFFIX
int dp[MS][]; // RMQ
int t1[MS],t2[MS],c[MS],v[MS];
int rank[MS],sa[MS],height[MS];
char str[MS],str1[MS];
int s[MS];
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
} void get_sa(int *r,int *sa,int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[x[i]=r[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[x[i]]]=i;
p=;j=;
for(;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++)
y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=;i<n;i++)
v[i]=x[y[i]];
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[v[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[v[i]]]=y[i];
swap(x,y);
x[sa[]]=;
for(p=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
} void get_height(int *r,int n)
{
int i,j,k=;
for(i=;i<=n;i++)
rank[sa[i]]=i;
//height[i]>=height[i-1]-1;
for(i=;i<n;i++)
{
if(k)
k--;
else
k=;
j=sa[rank[i]-];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} int main()
{
scanf("%s%s",str,str1);
int n=,len=strlen(str);
for(int i=;i<len;i++)
s[n++]=str[i]-'a'+;
s[n++]=; // 分隔符
len=strlen(str1);
for(int i=;i<len;i++)
s[n++]=str1[i]-'a'+;
s[n]=; // 为了方便比较
get_sa(s,sa,n+,);
get_height(s,n);
int maxv=;
len=strlen(str);
for(int i=;i<n;i++)
{
if(height[i]>maxv)
{
if(<=sa[i-]&&sa[i-]<len&&len<sa[i])
maxv=height[i];
if(<=sa[i]&&sa[i]<len&&len<sa[i-])
maxv=height[i];
}
}
printf("%d\n",maxv);
return ;
}

Long Long Message 后缀数组入门题的更多相关文章

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

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

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

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

  3. BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题

    BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...

  4. 后缀数组入门(二)——Height数组与LCP

    前言 看这篇博客前,先去了解一下后缀数组的基本操作吧:后缀数组入门(一)--后缀排序. 这篇博客的内容,主要建立于后缀排序的基础之上,进一步研究一个\(Height\)数组以及如何求\(LCP\). ...

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

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

  6. POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)

    后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...

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

  8. POJ 2774 Long Long Message 后缀数组

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

  9. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

随机推荐

  1. C++的双冒号(域解析符)

    在C++中,“::”表示“作用域标识符”或者叫“作用域分解运算符”,比如:“类名::函数名”,这样是表示该函数是该类的成员函数, 但是象下面这种写法:“::函数名”,作用域标识符前面没有任何对象,代表 ...

  2. 正则表达式匹配中文字符串的文章URL

    http://www.cnblogs.com/yitian/archive/2008/11/14/1333569.html

  3. C#: 方法的默认参数

    大家都知道在C++中,我们可以为方法的参数指定一个默认值,像这样: void foo(int i = 100); 当我们以这种形式调用方法的时候: foo(); 实际上参数i被赋于了默认值,所以相当于 ...

  4. linux TCP数据包重传过程----小结

    于TCP/IP协议栈的TCP协议的重传功能是由在linux内核源码(net/ipv4/tcp_output.c)中的函数tcp_retransmit_skb()实现的 代码如下: /* This re ...

  5. UI:tomcat(说话小程序)、相框动画、UISgmentcontrol、UISwitch

    UISegmentedControl 分段控件 //1. UISegmentedControl 分段控件 (就是一个一个的按钮) //分段显示的标题是以一个数组存储的 NSArray * titles ...

  6. 导入flash 逐帧动画

    只要图片是一序列的,比如0001-0100,就可以使用导入素材功能,选择第一张图片,选中影片剪辑并编辑然后点击导入到舞台,然后FlashCS6工具就会问你,这是一序列图片,是否逐帧导入,点是,就ok了 ...

  7. JAVA核心技术--继承

    1.继承:向上追溯,对同一批类的抽象,延续和扩展父类的一切信息! 1)关键字:extends      例如,父类是Animal,子类是Dog;   eg: public class Dog exte ...

  8. Hadoop on Mac with IntelliJ IDEA - 9 解决Type mismatch in value from map问题

    修改陆喜恒. Hadoop实战(第2版)5.3排序的代码时遇到IO异常. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Hadoop 1.2.1 异常具体信息如下 ...

  9. 反射遍历List<>泛型类型

    有这样一个需求:一个实体模型,有一个属性是一个实体列表List<OrderDetail>,我需要对实体列表遍历,对每一个实体的属性根据属性特性进行验证,如是否为必填等等,如下一个反射方法能 ...

  10. Codeforces Round #260 (Div. 1) D. Serega and Fun 分块

    D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...