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 题意就是求两个字符串 ...
随机推荐
- laravel 开发辅助工具
laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...
- 【Codeforces 493D】Vasya and Chess
[链接] 我是链接,点我呀:) [题意] [题解] 会发现两个皇后之间如果只有奇数个位置 也就是n%2==1 那么第二个人总是赢的 因为如果white往下跑的话,black也能往下跑. 第二个人没有输 ...
- http://localhost/main/company/jurtion---外卖权限添加
http://localhost/main/company/jurtion---外卖权限添加
- ceph rbd 入门
1.一个现成的ceph cluster 参考之前写的ceph-deploy 部署ceph cluster 2.配置client与ceph cluster对接 在ceph cluster的管理节点上安装 ...
- nyoj 5 Binary String Matching(string)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 线段树题集 (cf版)
lazy区间修改 : http://acm.hdu.edu.cn/showproblem.php?pid=4902 (hdu4902) http://acm.hdu.edu.cn/showpr ...
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- MYSQL常用的性能指标
(1) QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Questio ...
- java ee标准DataSource理解
- MongoDB:最简单的增删改查(Oops,可能太简单了)
热身运动: 1. 启动MongoDB shell => mongo.exe or ./mongo(先确保有一个mongod的实例): 2. 切换到一个用于练手的namespace => u ...