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 题意就是求两个字符串 ...
随机推荐
- python3 判断大小写
转自http://wangwei007.blog.51cto.com/68019/1134323 # 一.pyhton字符串的大小写转换, 常用的有以下几种方法: # 1.对字符串中所有字符(仅对字母 ...
- scanf与getchar
如下: 5 5 R R R R R R R R R R R R R R R R R R R R R R R R R 只允许用scanf,如何写读取函数. 由于 ...
- Windows学习总结(10)——Windows系统中常用的CMD命令详解
1.ping命令 ping是电脑网络故障诊断中的常用的命令,它的作用是用来检查网络是否通畅或者网络连接速度.我们来看一下PING命令的具体表述. 日常的诊断过程中我们最常用到的就是诊断连接是否通畅. ...
- CODEVS1022 覆盖 (二分图染色+匈牙利算法)
先对整幅图进行二分图染色,再跑一遍匈牙利算法. /* CODEVS1022 */ #include <cstdio> #include <cstring> #include & ...
- [bzoj1022/poj3480]小约翰的游戏John_博弈论
小约翰的游戏John 题目大意:Nim游戏.区别在于取走最后一颗石子这输. 注释:$1\le cases \le 500$,$1\le n\le 50$. 想法:anti-SG游戏Colon定理. 如 ...
- - > 最大公约数(辗转相除法)和最小公倍数(公式法)
最大公约数 #include<iostream> using namespace std; int a,b; int gcd(int x,int y){ return x==0?y:gcd ...
- 线程调度策略SCHED_RR(轮转法)和SCHED_FIFO(先进先出)之对照
我们在用pthread创建线程时,能够指定调度策略policy--SCHED_OTHER(默认).SCHED_RR和SCHED_FIFO.这里TALK一下两个实时策略--SCHED_RR和SCHED_ ...
- 这篇讲angular 的$q的讲得不错
原文: https://segmentfault.com/a/1190000000402555 ---------------------------------------------------- ...
- ssh连接断开后 shell进程退出
问题描述:当SSH远程连接到服务器上,然后运行一个服务 ./catalina.sh start,然后把终端开闭(切断SSH连接)之后,发现该服务中断,导致网页无法访问. 解决方法:使用nohup命 ...
- 最全Pycharm教程(37)——Pycharm版本号控制之基础篇
1.主题 介绍Pycharm的版本号控制系统 2.准备工作 (1)Pycharm版本号为2.7或者更高 (2)已经创建一个project.參见Getting Started tutorial (3)安 ...