POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串
题目链接:https://vjudge.net/problem/POJ-2774
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 33144 | Accepted: 13344 | |
Case Time Limit: 1000MS |
Description
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
Output
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output
27
Source
题意:
求两个字符串的最长公共子串。
题解(后缀数组):
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 —— 后缀数组 两字符串的最长公共子串的更多相关文章
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- poj2774 Long Long Message(后缀数组or后缀自动机)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Long Long Message Time Limit: 4000MS Me ...
- poj2774 Long Long Message 后缀数组求最长公共子串
题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...
- 求两个字符串的最长公共子串——Java实现
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...
- POJ2774 Long Long Message [后缀数组]
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 29277 Accepted: 11 ...
- poj2774 后缀数组2个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 26601 Accepted: 10 ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...
随机推荐
- Oracle递归操作
需求:找出代理商中没有挂商家的代理商 简单SQL如下: select * from t_proxy tp where tp.id not in (SELECT tp.id as p_id FROM t ...
- [转]Windows10内置Linux子系统初体验
Windows10内置Linux子系统初体验 https://www.jianshu.com/p/bc38ed12da1d
- 如何选择Haproxy和Nginx
对于做软负载,我们都知道主流的方案有LVS.Haproxy.Nginx!那么对于Haproxy和Nginx,我们如何选择呢?回答这个问题之前,我根据个人使用经验来讲下它们的特点! Haproxy特点 ...
- Nginx学习——Nginx进程间的通信
nginx进程间的通信 进程间消息传递 共享内存 共享内存还是Linux下提供的最主要的进程间通信方式,它通过mmap和shmget系统调用在内存中创建了一块连续的线性地址空间,而通过munmap或者 ...
- angular - 如何支持less和sass(scss)
更新时间: (2018-7-26) - 使用angular6.x最新版本 新建项目时,我们指定类型: 示例:ng new projectname -style=sass(scss) 实例:ng new ...
- doubango库改动Contact内容的地方
要在手机client中实现会议功能,从音频会议開始(可能会优点理点).server用的是Centos7 64 + Freeswitch1.4.client是基于doubango的imsdroid.还有 ...
- 一步一步实现一个简单的OS(简单的让boot载入setup)
这次直接写用boot载入setup模块. 文件系统就先不弄了,以后再说, 咱先整个转简单的载入器. 我把软盘引导改成硬盘了,由于硬盘的读扇区函数简单一些. 这里没有做硬盘的mbr区,我认为在如今我的这 ...
- ubuntu16.04----jdk---install----config
1.下载jdk. 2.验证java是否安装,使用java -version命令,如下图所示说明没有安装: 3.在usr目录中创建一个jdk-8目录,如下图所示: 4.配置系统环境变量,编辑/etc/p ...
- c++引用返回值
引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...
- 关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题
关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题,有需要的朋友可以参考下. php调用java写的soap接口经验: 场景一: java是以数组的形式接收参数的 ...