CODE【VS】 3160 最长公共子串 (后缀数组)
3160 最长公共子串
题目描述 Description
给出两个由小写字母组成的字符串,求它们的最长公共子串的长度。
输入描述 Input Description
读入两个字符串
输出描述 Output Description
输出最长公共子串的长度
样例输入(Sample Input)
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
样例输出(Sample Output)
27
数据范围及提示
单个字符串的长度不超过100000
将两个串链接成一个串,之后直接求hight数组即可,同时要求:
- 两个后缀只来自各自的字符串
这一点只要在中间加个特殊字符即可,因为只要使得两个后缀的起始点来自于不同串,特殊字符会使得他们在求lcp时断开
/*
作者:Acforgood
题目:p3160 最长公共子串
*/
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 300
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=200005;
int n;
char ch[N];
int a[N],h[N];
int v[N];
int sa[2][N],rk[2][N];
int p,q,k;
void calsa(int sa[N],int rk[N],int SA[N],int RK[N])
{
for(int i=1; i<=n; i++)v[rk[sa[i]]]=i;
for(int i=n; i; i--)
if(sa[i]>k)
SA[v[rk[sa[i]-k]]--]=sa[i]-k;
for(int i=n-k+1; i<=n; i++)SA[v[rk[i]]--]=i;
for(int i=1; i<=n; i++)
RK[SA[i]]=RK[SA[i-1]]+(rk[SA[i]]!=rk[SA[i-1]]||rk[SA[i]+k]!=rk[SA[i-1]+k]);
}
void work()
{
p=0,q=1;
for(int i=1; i<=n; i++)v[a[i]]++;
for(int i=1; i<=30; i++)v[i]+=v[i-1];
for(int i=1; i<=n; i++)
sa[p][v[a[i]]--]=i;
for(int i=1; i<=n; i++)
rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+(a[sa[p][i]]!=a[sa[p][i-1]]);
for(k=1; k<n; k<<=1)
{
calsa(sa[p],rk[p],sa[q],rk[q]);
swap(p,q);
}
}
void geth()
{
k=0;
for(int i=1; i<=n; i++)
if(rk[p][i]==1)h[rk[p][i]]=0;
else
{
int j=sa[p][rk[p][i]-1];
while(a[i+k]==a[j+k])k++;
h[rk[p][i]]=k;
if(k>0)k--;
}
}
int main()
{
while(~scanf("%s",ch+1))
{
n=strlen(ch+1);
int len=n+1;
ch[n+1]='a'-1;
scanf("%s",ch+n+2);
n=strlen(ch+1);
for(int i=1; i<=n; i++)a[i]=ch[i]-'a'+1;
work();
geth();
int ans=0;
for(int i=1; i<=n; i++)
{
if((sa[p][i]>len&&sa[p][i-1]<len)||(sa[p][i]<len&&sa[p][i-1]>len)) ans=max(ans,h[i]);
}
printf("%d\n",ans);
}
return 0;
}
CODE【VS】 3160 最长公共子串 (后缀数组)的更多相关文章
- Codevs 3160 最长公共子串(后缀数组)
3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...
- poj 2774 最长公共子串 后缀数组
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 25752 Accepted: 10 ...
- POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total ...
- 【codevs3160】最长公共子串 后缀数组
题目描述 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入 读入两个字符串 输出 输出最长公共子串的长度 样例输入 yeshowmuchiloveyoumydearmotherrea ...
- codevs 3160 最长公共子串 后缀自动机
http://codevs.cn/problem/3160/ 后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j]. ...
- CODE【VS】3160 最长公共子串 (后缀自动机)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...
- codevs 3160 最长公共子串(SAM)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...
- poj 1743 Musical Theme(最长重复子串 后缀数组)
poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...
随机推荐
- pyinstaller打包.py程序为.exe操作指南
pyinstaller打包.py程序为.exe操作指南 20190526内容纲要: 1.pyinstaller安装 2.程序封装 3.可执行程序 0 前言 今天第一次试试将一个py程序封装成一个.ex ...
- 升级Python后, yum不能用了
yum需要使用python2,而升级python3后,就会导致语法错误 更正方法: sudo vim /usr/bin/yum 然后把第一行的python改成python2就好了 之后如果出现类似的, ...
- 使用pabot并行执行robotframework用例
主要观点:使用pabot并行运行robotframework,可以解决:robotframework执行案例时间长的问题 解决执行案例时间长的方案: 目的: 缩短案例的运行时间 两种方法: 将大的项目 ...
- WIN7中Beyond Compare报错误“应用程序发生错误” 无法启动
BCompare在WIN7中打开提示"应用程序发生错误"的解决办法: WIN7下寻找:把C:\用户\[用户名]\AppData\Roaming\Scooter Software\B ...
- 利用Marshal.AllocHGlobal申请非托管内存,unsafe代码
unsafe public class RUN { int[] array3; IntPtr handle; ; public RUN() { handleCount = * ; handle = S ...
- Android开发学习——android与服务器端数据交互
1.首先搭建服务器端. 使用MyEclipse开发工具 public class MyServlet extends HttpServlet { @Override protected void do ...
- JAVA300集笔记
章节2 java入门阶段 2.1注释 单行注释 // 多行注释 /* 内容*/ 文本注释/**内容*/ 注释是为了方便阅读代码,在编译时注释会被删除. 2.2 标识符 标识符作用: 标识符用来给变量 ...
- iOS 中集成海康威视 摄像视频
本文原文地址 http://www.cnblogs.com/qianLL/p/6652104.html 一.要导入相关的库,注意 这里比较坑的是 要用和他一样的 如果开始的工程中用了AFN或者MJE ...
- Node.js(二)常用的系统模块
http模块 第一章已经介绍了 node.js 的模块都可以传一个回调函数 回调函数支持两个参数 error , data let fs = require('fs'); fs.readFile( ...
- JavaScript——XMLHttpRequest 家族
https://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arrayb ...