题目链接:https://vjudge.net/problem/POJ-2774

Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 33144   Accepted: 13344
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."

题意:

求两个字符串的最长公共子串。

题解(后缀数组):

1.将两个字符串拼接在一起,中间用特殊字符隔开。然后求后缀数组。

2.枚举height数组:对于height[i],如果sa[i]、sa[i-1]分别位于两个字符串中,那么height[i]即为两个字符串的公共子串,求最大值即可。

后缀数组

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(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<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]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} char a[MAXN], b[MAXN];
int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int main()
{
while(scanf("%s", a)!=EOF)
{
scanf("%s", b);
int lena = strlen(a);
int lenb = strlen(b);
int len = ;
for(int i = ; i<lena; i++) r[len++] = a[i];
r[len++] = ;
for(int i = ; i<lenb; i++) r[len++] = b[i];
r[len] = ;
DA(r, sa, Rank, height, len, ); int ans = ;
for(int i = ; i<=len; i++)
if((sa[i-]<=lena&&sa[i]>lena)||(sa[i-]>lena&&sa[i]<=lena))
ans = max(ans, height[i]);
printf("%d\n", ans);
}
return ;
}

二分 + 字符串哈希:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; char a[MAXN], b[MAXN];
int n, m;
LL Map[MAXN], bit[MAXN], seed = ;
bool test(int k)
{
Map[] = ;
for(int i = ; i<k; i++)
Map[] *= seed, Map[] += a[i];
for(int i = k; i<n; i++)
Map[i-k+] = Map[i-k]*seed - 1LL*a[i-k]*bit[k] + 1LL*a[i];
int cnt = n-k+;
sort(Map, Map+cnt); LL tmp = ;
for(int i = ; i<k; i++)
tmp *= seed, tmp += b[i];
if(binary_search(Map,Map+cnt,tmp)) return true;
for(int i = k; i<m; i++)
{
tmp = tmp*seed - 1LL*b[i-k]*bit[k] + 1LL*b[i];
if(binary_search(Map,Map+cnt,tmp))
return true;
}
return false;
} int main()
{
scanf("%s%s", a, b);
n = strlen(a);
m = strlen(b); bit[] = ;
for(int i = ; i<n; i++)
bit[i] = bit[i-]*seed;
int l = , r = min(n, m);
while(l<=r)
{
int mid = (l+r)/;
if(test(mid))
l = mid + ;
else
r = mid - ;
}
printf("%d\n", r);
}

POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串的更多相关文章

  1. 利用后缀数组(suffix array)求最长公共子串(longest common substring)

    摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...

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

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

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

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

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

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

  5. 求两个字符串的最长公共子串——Java实现

    要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...

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

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

  7. poj2774 后缀数组2个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 26601   Accepted: 10 ...

  8. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  9. URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...

随机推荐

  1. Octave入门基础

    Octave入门基础 一.简单介绍 1.1 Octave是什么? Octave是一款用于数值计算和画图的开源软件.和Matlab一样,Octave 尤其精于矩阵运算:求解联立方程组.计算矩阵特征值和特 ...

  2. AngularJS中选择样式

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSSelectClass.rar 要点,{{ctrl.name}}比<spa ...

  3. iptables和DNS

    1.iptables防火墙 表→链→规则 filter表 数据过滤表 NAT表---内网和外网的地址转换 Mangle-----数据流量,通过防火墙设置流量.特殊数据包标记.太复杂,一般不用.限速工具 ...

  4. Spket安装

    1.http://www.spket.com/下载1.6.23放到Eclipse或者myEclipse的dropins文件夹下 2. 3. 4. 我的是jquery-1.8.2.js,2.1.1不会提 ...

  5. mysql中UNIX_TIMESTAMP()函数和php中time()函数的区别

    http://tech.ddvip.com/2009-01/1231392775105351.html mysql 中:UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date) 若 ...

  6. 高阶函数:sorted

    排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的 ...

  7. deeplearning.net 0.1 document - Multilayer Perceptron

    Multilayer Perceptron 以下我们使用Theano来介绍一下单隐藏层的多层感知机(MLP).MLP能够看成一个logistic回归分类器,它使用一个已经学习的非线性转换器处理输入.这 ...

  8. Hollis原创|不了解这12个语法糖,别说你会Java

    GitHub 2.5k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 2.5k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 2.5k Star 的 ...

  9. vue proxyTable

    Vue-cli proxyTable 解决开发环境的跨域问题 字数474 阅读1685 评论1 喜欢3 和后端联调时总是会面对恼人的跨域问题,最近基于Vue开发项目时也遇到了这个问题,两边各自想了一堆 ...

  10. Ubuntu下安装和编译ffmpeg

    参考:http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu   1.安装依赖包 sudo apt-get update sudo apt-get -y ...