【SPOJ】Longest Common Substring
【SPOJ】Longest Common Substring
求两个字符串的最长公共子串
对一个串建好后缀自动机然后暴力跑一下 废话
讲一下怎么跑吧
从第一个字符开始遍历,遍历不到了再沿着\(parents\)走看能否找到出路,走到某个点时,统计一下走过了多少点然后更新答案
来说说这样做的正确性:
遍历是肯定的, SAM 从根节点出发的任意路径都表示一个子串
沿着\(parents\)边往后走,保证贪心情况下维护最长公共子串寻找出路
注意这里是统计走过了多少点更新答案,不能直接通过\(len\)更新答案
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=600000;
LL nod,last,n,T;
LL len[maxn],fail[maxn],son[maxn][26];
char s[maxn];
inline void Insert(LL c){
LL np=++nod,p=last;
len[np]=len[p]+1;
last=np;
while(p&&!son[p][c]){
son[p][c]=np,
p=fail[p];
}
if(!p)
fail[np]=1;
else{
LL q=son[p][c];
if(len[q]==len[p]+1)
fail[np]=q;
else{
LL nq=++nod;
len[nq]=len[p]+1;
fail[nq]=fail[q];
memcpy(son[nq],son[q],sizeof(son[q]));
fail[np]=fail[q]=nq;
while(p&&son[p][c]==q){
son[p][c]=nq,
p=fail[p];
}
}
}
}
int main(){
nod=last=1;
scanf(" %s",s);
LL Len=strlen(s);
for(LL i=0;i<Len;++i)
Insert(s[i]-'a');
scanf(" %s",s);
LL ans=0,now=1,cnt=0;
Len=strlen(s);
for(LL i=0;i<Len;++i){
LL c=s[i]-'a';
if(son[now][c])
++cnt,
now=son[now][c];
else{
while(now&&!son[now][c])
now=fail[now];
if(!now)
cnt=0,
now=1;
else
cnt=len[now]+1,
now=son[now][c];
}
ans=max(ans,cnt);
}
printf("%lld\n",ans);
return 0;
}/*
fjewiofejhiofjmwopejeugfzjkjnfoakweldnfmoierhguiewkjfkowejrfoiwejsfd
jwierhdwuiek,dedjfkz[pjeowrfhuqigrfwerljfiuekdfkcdfheosf
*/
【SPOJ】Longest Common Substring的更多相关文章
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- 【SPOJ1811】Longest Common Substring(后缀自动机)
题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度 n<=250000 思路: #include<bits/stdc++.h> using namespace std; t ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- SPOJ 1812 Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- Linux入职基础-1.1_国内开源的主要镜像站
Linux入职基础-1.1_国内开源的主要镜像站 东北地区: 东北大学 http://mirror.neu.edu.cn 大连理工大学 http://mirror.dlut.edu.cn 大连东软 ...
- js入门之DOM动态创建数据
一. 知识点回顾 1. DOM结构 nodeName: '' 标签名 nodeType: '' 类型 1元素节点 2属性节点 3文本节点 nodeValue: '' 如果是元素节点 nodeValue ...
- shell 大型脚本工具开发实战
拆分脚本功能,抽象函数 1.function get_all_group 返回进程组列表字符串 2.function get_all_process 返回进程名列表字符串"nginx htt ...
- 删除MRP单据
select *into newtable from a_mplist 把a_mplist的表中的数据复制到newtable表中结构也是一样的 insert into newtable select ...
- Computer Vision_33_SIFT:An Improved RANSAC based on the Scale Variation Homogeneity——2016
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- thinkphp5自动生成文档/注释代码自动生成api文档
composer require weiwei/api-doc dev-master 安装之后,readme 有详细的使用说明代码: 部分界面: gitbub:https://github.com/z ...
- AD19新功能之ActiveRoute
AD19新增了ActiveRoute功能(自动布线功能),强大而实用: 1.河流式布线模式 在PCB面板中选择相应的网络,选中飞线,或者按住 Alt 然后鼠标从右下往左上进行框选,选中对应的飞线 然后 ...
- (转)大厂常问到的14个Java面试题
1. synchronized和reentrantlock异同 相同点 都实现了多线程同步和内存可见性语义 都是可重入锁 不同点 实现机制不同 synchronized通过java对象头锁标记和Mon ...
- IDEA实用教程(三)
4. JDK环境的配置 1) 进入JDK配置界面 2) 创建JDK环境 3) 选择本地JDK的安装位置 4) 保存配置 点击右下角的Apply后,再点击OK保存配置
- 【记忆化搜索】[NOIP-2017--普及组] -- 棋盘
[题目描述] 原题目链接地址: 有一个m × m的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的( ...