poj 2774 后缀数组 两个字符串的最长公共子串
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 31904 | Accepted: 12876 | |
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
//论文题,一个字符串的最长重复子串就是heigh数组的最大值,这里把后一个字符串接到前一个的前面中间用一个字符分开,然后求不在
//同一个字符串中的最大的heigh数组值。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int sa[MAXN+],he[MAXN+],ra[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+],f[MAXN+][];
char s[MAXN+];
int len,m;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=; continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
int solve(int len1)
{
int ans=;
for(int i=;i<len;i++){
if(he[i]>ans&&((sa[i]>len1&&sa[i-]<len1)||(sa[i]<len1&&sa[i-]>len1)))
ans=he[i];
}
return ans;
}
int main()
{
scanf("%s",s);
int len1=strlen(s);
s[len1]='#';
scanf("%s",s+len1+);
len=strlen(s);
m=;
get_suf();
printf("%d\n",solve(len1));
return ;
}
poj 2774 后缀数组 两个字符串的最长公共子串的更多相关文章
- poj2774 后缀数组2个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 26601 Accepted: 10 ...
- 求两个字符串的最长公共子串——Java实现
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...
- [URAL-1517][求两个字符串的最长公共子串]
Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...
- poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数
Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11469 Accepted: 379 ...
- luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串
题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...
- POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串
题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS Memory Limit: 10000K Total ...
- 【Java例题】5.5 两个字符串中最长公共子串
5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...
- hihocoder-1415 后缀数组三·重复旋律3 两个字符串的最长公共子串
把s1,s2拼接,求Height.相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min. #include <iostream> ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
随机推荐
- python判断文件和文件夹是否存在、没有则创建文件夹
原文出处:https://www.cnblogs.com/hushaojun/p/4533241.html >>> import os >>> os.path.ex ...
- roadhog中如何拷贝文件
一:使用 public 目录 我们约定 public 目录下的文件会在 server 和 build 时被自动 copy 到输出目录(默认是 ./dist)下.所以可以在这里存放 favicon, i ...
- oracle删除死锁进程
在命令行下运行: select SID,SERIAL# from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; alte ...
- 第10章 系统级I/O(下)
10.7 I/O重定向 Unix外壳提供了I/O重定向操作符,允许用户将磁盘文件和标准输出输入联系起来. 例如:unix>ls>foo.txt,使得外壳加载和执行ls程序,将标准输出重定 ...
- Sprint4
进展:今天一天满课,晚上也没有做什么,所以今天一天没什么进展. 燃尽图:
- Task 4.5 求二维数组中的最大连通子数组之和
任务:输入一个二维整形数组,数组里有正数也有负数. 求所有子数组的和的最大值.要求时间复杂度为O(n). 1.设计思想:因为用之前的解决子数组最大和的问题的思路一直没能解决这个问题,后来看到同学使用将 ...
- beat冲刺(5/7)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(5/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 ppt制作中 数据集标注 接下来的计划 制作p ...
- ACM的fflush(stdin)的问题
在最近的刷题过程中,因为用到了很多字符串的操作,有时需要多次清空缓冲区,所以用了fflush(stdin);的语句,确实很好用,但是发现在OJ上提交后会出现runtime error的问题.当时并没有 ...
- 关于mysqlbinlog的重要性.
今天在做update更新数据的时候,因为没有统一好需要更新的数据编执行了update操作,所以需要回滚到先前的数据,所以就赶紧去服务器看binlog日志,结果一看binlog竟然没有开启,把我给惊的啊 ...
- IDEA 开发工具的快捷键
IDEA 开发工具的快捷键 原文链接:http://blog.csdn.net/wfp458113181wfp/article/details/24579781 1.文本编辑 删除 ctr + ...