codevs 3160 最长公共子串 后缀自动机
http://codevs.cn/problem/3160/
后缀自动机板子题,匹配的时候要注意如果到一个点失配向前匹配到一个点时,此时的tmp(当前匹配值)为t[j].len+1而不是t[t[j].sig[z]].len,想一下自动机的特质就可以明白了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=;
char ch1[maxn]={},ch2[maxn]={};
int siz1,siz2;
struct nod{
int sig[];
int f,len;
}t[maxn*];int tot=,la=;
void add(int z){
int x=++tot;int i=la;
t[x].len=t[la].len+;
for(;i&&!t[i].sig[z];i=t[i].f)
t[i].sig[z]=x;
if(!i)t[x].f=;
else{
int p=t[i].sig[z];
if(t[p].len==t[i].len+)t[x].f=p;
else{
int y=++tot;
t[y]=t[p];t[y].len=t[i].len+;
t[x].f=t[p].f=y;
for(;i&&t[i].sig[z]==p;i=t[i].f){
t[i].sig[z]=y;
}
}
}
la=x;
}
int main(){
memset(t,,sizeof(t));
scanf("%s",ch1+);
scanf("%s",ch2+);
siz1=strlen(ch1+);
siz2=strlen(ch2+);
for(int i=;i<=siz1;i++)add(int(ch1[i]-'a'));
int ans=,j=,tmp=;
for(int i=;i<=siz2;i++){
int z=ch2[i]-'a';
if(t[j].sig[z]){j=t[j].sig[z];tmp++;}
else{
while(j&&!t[j].sig[z])
j=t[j].f;
if(!j){j=;tmp=;}
else {tmp=t[j].len+;j=t[j].sig[z];}
}
if(tmp>ans)ans=tmp;
}printf("%d\n",ans);
return ;
}
codevs 3160 最长公共子串 后缀自动机的更多相关文章
- Codevs 3160 最长公共子串(后缀数组)
3160 最长公共子串 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长 ...
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...
- CODE【VS】 3160 最长公共子串 (后缀数组)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- codevs 3160 最长公共子串(SAM)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...
- SCOJ 4493: DNA 最长公共子串 后缀自动机
4493: DNA 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4493 Description Deoxyribonucleic acid ( ...
- CODE【VS】3160 最长公共子串 (后缀自动机)
3160 最长公共子串 题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Outp ...
- 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的.. ...
随机推荐
- Verilog笔记.1.基本语法
0.前 抽象模型分级: • 系统级(system):用高级语言结构实现设计模块的外部性能的模型.• 算法级(algorithm):用高级语言结构实现设计算法的模型.• RTL级(Register Tr ...
- WAMP允许外部访问的修改方法
apache配置文件httpd.conf里的 "Require local"改" Require all granted"
- 一个爬取https和http通用的工具类(JDK自带的URL的用法)
今天在java爬取天猫的时候因为ssl报错,所以从网上找了一个可以爬取https和http通用的工具类.但是有的时候此工具类爬到的数据不全,此处不得不说python爬虫很厉害. package cn. ...
- Perl6多线程3: Promise start / in / await
创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...
- Linux下的压缩解压缩
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲t ...
- appium===浮窗无法定位的解决办法
这个问题比较常见,而且不被重视.大多数人的解决思路是根据坐标,把它点掉. 如下图,根本无法定位出“你好”这个元素: 最后的解决办法是: 在desired_caps={}中增加, desired_cap ...
- shell读取文件的每一行内容并输出【转】
写法一: #!/bin/bash while read line do echo $line done < file(待读取的文件) 写法二: #!/bin/bash cat file(待读取的 ...
- oracle日期格式转换 to_date()
与date操作关系最大的就是两个转换函数:to_date(),to_char() to_date() 作用将字符类型按一定格式转化为日期类型: 具体用法:to_date(''2 ...
- POJ 1661 Help Jimmy(二维DP)
题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...
- js自动检索输入文章长度
1. 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...