SPOJ 1811 SAM 初探
思路:
一个串建SAM
另一个串在SAM上跑
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=;
int n;char a[N],b[N];
struct SAM{
int ch[N][],dis[N],fa[N],root,tot,last;
void init(){root=tot=last=;}
int newnode(int v){return dis[++tot]=v,tot;}
void add(int x){
int p=last,np=newnode(dis[p]+);last=np;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=root;
else{
int q=ch[p][x];
if(dis[q]==dis[p]+)fa[np]=q;
else{
int nq=newnode(dis[p]+);
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q],fa[np]=fa[q]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
}T;
int main(){
T.init();
scanf("%s%s",a+,b+);
int n=strlen(a+),m=strlen(b+),ans=,len=;
for(int i=;i<=n;i++)T.add(a[i]-'a');
int p=T.root;
for(int i=;i<=m;i++){
int x=b[i]-'a';
if(T.ch[p][x])len++,p=T.ch[p][x];
else{
for(;p&&!T.ch[p][x];p=T.fa[p]);
if(!p)p=T.root,len=;
else len=T.dis[p]+,p=T.ch[p][x];
}ans=max(ans,len);
}printf("%d\n",ans);
}
SPOJ 1811 SAM 初探的更多相关文章
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
- SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)
http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...
- 【刷题】SPOJ 1811 LCS - Longest Common Substring
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SAM初探
SAM,即Suffix Automaton,后缀自动机. 关于字符串有很多玩法,有很多算法都是围绕字符串展开的.为什么?我的理解是:相较于数字组成的序列,字母组成的序列中每个单位上元素的个数是有限的. ...
- SPOJ 1811 Longest Common Substring
Description 给出两个字符串,求最长公共子串. Sol SAM. 这题随便做啊...后缀数组/Hash+二分都可以. SAM就是模板啊...直接在SAM上跑就行,没有了 \(go[w]\) ...
- SPOJ 1811 Longest Common Substring 后缀自动机
模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...
随机推荐
- buf.readIntBE()
buf.readIntBE(offset, byteLength[, noAssert]) buf.readIntLE(offset, byteLength[, noAssert]) offset { ...
- Volume 6. Mathematical Concepts and Methods
138 - Street Numbers #include <stdio.h> //(2n+1)^2 - 8m^2 = 1,佩尔函数,或者打表 int main() { ; , xi = ...
- Python-组合数据类型
集合类型及操作 >集合类型定义 集合是多个元素的无序组合 -集合类型与数学中的集合概念一致 -集合元素之间无序,每个元素唯一,不存在相同元素 -集合元素不可更改,不能是可变数据类型 -集合用大括 ...
- 【01】bootstrap基本信息
[01]基本信息 中文官网:http://www.bootcss.com/ 英文官网:https://github.com/twbs/bootstrap/ 支持IE8+ CND : htt ...
- 【Codeforces 411A】Password Check
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 傻逼模拟题 [代码] import java.io.*; import java.util.*; public class Main { st ...
- Android三角标签View:TriangleLabelView
Android三角标签View:TriangleLabelView 在一些商城.产品推销类APP中,如淘宝.京东.电影门票销售.商品降价促销这类的APP,常常会在其APP中看到,某些商品的左上角 ...
- 【GC分析】Java GC日志查看
Java中的GC有哪几种类型? 参数 描述 UseSerialGC 虚拟机运行在Client模式的默认值,打开此开关参数后, 使用Serial+Serial Old收集器组合进行垃圾收集. UsePa ...
- RSYNC最简实施
只是内网同步,故而可以省略很多安全方面的东东.不需要通过ssh,而是通过rsync协议.不需要用户名认证,保证只读. rsync用standalone的daemon方式,而不用service方式操作. ...
- kendo grid 报错:length
其实是:events中的{}Onsave的问题,把events整个注释掉就好了
- P1009 阶乘之和 洛谷
https://www.luogu.org/problem/show?pid=1009 题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=54321. ...