CODE【VS】3160 最长公共子串 (后缀自动机)
3160 最长公共子串
题目描述 Description
给出两个由小写字母组成的字符串,求它们的最长公共子串的长度。
输入描述 Input Description
读入两个字符串
输出描述 Output Description
输出最长公共子串的长度
样例输入(Sample Input)
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
样例输出(Sample Output)
27
数据范围及提示
单个字符串的长度不超过100000
后缀自动机模版题,先以第一个串建立一个后缀自动机,第二个串直接匹配
#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=100005;
const int sigma=27;
int n;
char ch[N<<1];
struct sam{
int cnt,last;
int fa[N<<1],a[N<<1][sigma],mx[N<<1],len[N<<1];
sam(){
last=++cnt;
}
void extend(int c){
int p=last,np=last=++cnt;mx[np]=mx[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=fa[p];
if(!p)fa[np]=1;
else
{
int q=a[p][c];
if(mx[p]+1==mx[q])fa[np]=q;
else
{
int nq=++cnt;mx[nq]=mx[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=fa[p];
}
}
}
void solve(){
scanf("%s",ch+1);
memset(len,0,sizeof(len));
int l=strlen(ch+1),p=1,tmp=0,ans=0;
for(int i=1;i<=l;i++)
{
int c=ch[i]-'a';
while(!a[p][c]&&p)p=fa[p];
if(p==0)p=1,tmp=0;
else tmp=min(tmp,mx[p])+1,p=a[p][c];
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
}sam;
int main()
{
while(~scanf("%s",ch+1))
{
int n=strlen(ch+1);
for(int i=1;i<=n;i++) sam.extend(ch[i]-'a');
sam.solve();
}
return 0;
}
CODE【VS】3160 最长公共子串 (后缀自动机)的更多相关文章
- 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 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...
- SCOJ 4493: DNA 最长公共子串 后缀自动机
4493: DNA 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4493 Description Deoxyribonucleic acid ( ...
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...
- codevs 3160 最长公共子串(SAM)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...
- poj 2774 最长公共子串 后缀数组
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 25752 Accepted: 10 ...
- bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)
bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...
- 【wikioi】3160 最长公共子串(后缀自动机)
http://codevs.cn/problem/3160/ sam的裸题...(之前写了spoj上另一题sam的题目,但是spoj被卡评测现在还没评测完QAQ打算写那题题解时再来详细介绍sam的.. ...
随机推荐
- python爬虫之requests+selenium+BeautifulSoup
前言: 环境配置:windows64.python3.4 requests库基本操作: 1.安装:pip install requests 2.功能:使用 requests 发送网络请求,可以实现跟浏 ...
- 无法生成DH密钥对Could not generate DH keypair
Source from here Add this library to classpath(following is maven project) <dependency> < ...
- linux php5.6 提示 could not find driver
1.进入在PHP源码包中进入ext/pdo_mysql # wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 2.然后是解压缩. # tar -zxvf ...
- jsp错误处理
jsp提供了很好的错误能力,除了在java代码中可以使用try语句,还可以指定一个特殊页面,当页面应用遇到未捕获的异常时,用户将看到一个精心设计的网页解释发生了什么,而不是一个用户无法理解的错误信息. ...
- 工作记录 angular页面操作 MD5加密
今天只是做页面,基于angularjs,有美工做的图打底,确实好用 密码保存,用到了C# MD5加密: https://www.cnblogs.com/healer007/p/5062189.html
- scala基础篇-03 if与for
一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:
- 掌握Spark机器学习库-07-随机梯度下降
1)何为随机梯度下降 优化方法 迭代更新,来寻找函数全局最优解的方法 与最小二乘法相比:适用于变量众多,模型更复杂 2)梯度 变化最快,“陡峭” 通过函数表达式来衡量梯度 3)随机梯度下降原理推导过程 ...
- ECharts Map 属性详解
$(function() { // 路径配置 require.config({ paths : { // echarts: 'http://echarts.baidu.com/build/dist' ...
- 在 Windows Server 上搭建 *** 服务端(转载加亲测)
转载自:https://diveng.io/build-shadowsocks-server-on-windows-server.html 下面的教程建议大家使用第一种方法安装,说是比较简单.我则使用 ...
- jQuery动画处理
$(selector).hide(speed,callback);隐藏 $(selector).show(speed,callback);显示 $(selector).toggle(speed,cal ...