2018.12.15 spoj1812 Longest Common Substring(后缀自动机)
传送门
后缀自动机模板题。
题意简述:求两个字串的最长公共子串长度。
对其中一个构建后缀自动机,用另外一个在上面跑即可。
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
const int N=5e5+5;
int n;
char s[N];
struct SAM{
int tot,last,rt,len[N],son[N][26],link[N];
SAM(){tot=last=rt=1,len[0]=-1;fill(son[0],son[0]+26,1);}
inline void expand(int x){
int p=last,np=++tot;
last=np,len[np]=len[p]+1;
for(;p&&!son[p][x];p=link[p])son[p][x]=np;
if(!p){link[np]=rt;return;}
int q=son[p][x],nq;
if(len[p]+1==len[q]){link[np]=q;return;}
len[nq=++tot]=len[p]+1,memcpy(son[nq],son[q],sizeof(son[q])),link[nq]=link[q];
for(;p&&son[p][x]==q;p=link[p])son[p][x]=nq;
link[q]=link[np]=nq;
}
inline void query(){
int p=1,nowlen=0,mxlen=0;
for(ri i=1;i<=n;++i){
if(son[p][s[i]-'a']){p=son[p][s[i]-'a'],mxlen=max(mxlen,++nowlen);continue;}
while(!son[p][s[i]-'a'])p=link[p];
mxlen=max(mxlen,nowlen=len[p]+1),p=son[p][s[i]-'a'];
}
cout<<mxlen;
}
}sam;
int main(){
freopen("lx.in","r",stdin);
scanf("%s",s+1),n=strlen(s+1);
for(ri i=1;i<=n;++i)sam.expand(s[i]-'a');
scanf("%s",s+1),n=strlen(s+1),sam.query();
return 0;
}
2018.12.15 spoj1812 Longest Common Substring(后缀自动机)的更多相关文章
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 Longest Common Substring 后缀自动机
模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- 2018.12.15 bzoj3998: [TJOI2015]弦论(后缀自动机)
传送门 后缀自动机基础题. 求第kkk小的子串(有可能要求本质不同) 直接建出samsamsam,然后给每个状态赋值之后在上面贪心选最小的(过程可以类比主席树/平衡树的查询操作)即可. 代码: #in ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ1812 Longest Common Substring II
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
- SPOJ1812 - Longest Common Substring II(LCS2)
Portal,Portal to 洛谷 Description 给出\(n(n\leq10)\)个仅包含小写字母的字符串\(s_1..s_n(|s_i|\leq10^5)\),求这些字符串的最长公共子 ...
随机推荐
- Example of Formalising a Grammar for use with Lex & Yacc
Here is a sample of a data-file that we want to try and recognise. It is a list of students and info ...
- mysql中插入序列表
利用序列表来实现列转行:CREATE TABLE sequence(id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY)往里面插入数据 INSERT ...
- mac item2 ssh
一.常规ssh登录流程 ssh登陆有三个参数,主机名,用户名,用户密码,流程都是一样. 1.ssh 用户名@主机名 2.返回包含(yes/no)的字符串,此时输入 “yes" 3.然后再返回 ...
- js五种继承优缺点
//1.原型继承 //缺点: 当父级的属性有引用类型的时候,任意一个实例修改了这个属性,其他实例都会受影响 // 1)基本类型:Number Boolean String undefined null ...
- iOS - 抖音效果
抖音的转场动画—iOS https://www.jianshu.com/p/29b0165de712 抖音的上下滑实现—iOS https://www.jianshu.com/p/e8799510c7 ...
- List<Map<String, String>>和Map<String, List<String>>遍历
public void TestM() { List<Map<String, String>> lm = new ArrayList<>(); Ma ...
- 路径打印(set以及字符串的相关操作)
题目链接 题目描述 给你一串路径,譬如: a\b\c a\d\e b\cst d\ 你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样: a b ...
- Python数据分析--Pandas知识点(一)
本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘 1. 重复值的处理 利用drop_duplicates()函数删除数据表中重复多余的记录, 比如删除重复多余的ID. im ...
- Java反射获取对象VO的属性值(通过Getter方法)
有时候,需要动态获取对象的属性值. 比如,给你一个List,要你遍历这个List的对象的属性,而这个List里的对象并不固定.比如,这次User,下次可能是Company. e.g. 这次我需要做一个 ...
- PAT 1049 数列的片段和(20)(代码+思路分析)
1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...