传送门

后缀自动机模板题。

题意简述:求两个字串的最长公共子串长度。


对其中一个构建后缀自动机,用另外一个在上面跑即可。

代码:

#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(后缀自动机)的更多相关文章

  1. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

  2. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  3. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

  4. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  5. 2018.12.15 bzoj3998: [TJOI2015]弦论(后缀自动机)

    传送门 后缀自动机基础题. 求第kkk小的子串(有可能要求本质不同) 直接建出samsamsam,然后给每个状态赋值之后在上面贪心选最小的(过程可以类比主席树/平衡树的查询操作)即可. 代码: #in ...

  6. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  7. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  8. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

  9. SPOJ1812 - Longest Common Substring II(LCS2)

    Portal,Portal to 洛谷 Description 给出\(n(n\leq10)\)个仅包含小写字母的字符串\(s_1..s_n(|s_i|\leq10^5)\),求这些字符串的最长公共子 ...

随机推荐

  1. MySQL千万级数据分区存储及查询优化

    作为传统的关系型数据库,MySQL因其体积小.速度快.总体拥有成本低受到中小企业的热捧,但是对于大数据量(百万级以上)的操作显得有些力不从心,这里我结合之前开发的一个web系统来介绍一下MySQL数据 ...

  2. SVN集成compare4比较软件

    打开TortoiseSVN的Setting,选择左边的Diff Viewer 设置如下: "D:\Program Files\Beyond Compare 4\BComp.exe" ...

  3. 修改bootstrap-table中的分页样式

    使用bootstrap-table时,使用$("")选择器没办法选中下方的分页button按钮,可能跟它是动态生成的有关吧. 最终找到与之对应的js(bootstrap-table ...

  4. Python: 高阶函数与lambda表达式

    缘由: python语法简单一看就会,但用在实处,想因为少于实战,总感觉有些捉襟. 翻阅跟踪youtube_dl源码,看到filter()函数用法,及其中lambda表达式,感觉好有意思,就补下课,记 ...

  5. C++中的字符数组与字符指针

    //[C++基础]字符数组和字符指针.cpp//剑指offer上的这段话://为了节省内存,c/c++把常量字符串放到单独的一个内存空间.但是当几个指针赋值给相同的常量字符串时,它们实际上会指向相同的 ...

  6. ERROR: APK path is not specified for

    1. 打开project structure 2.设置outpath路径 最好为绝对路径 点击确定  重新编译即可. Note: Android Studio版本使用

  7. VML、SVG、Canvas简介

    1.VML: VML的全称是Vector Markup Language(矢量可标记语言),矢量的图形,意味着图形可以任意放大缩小而不损失图形的质量,这在制作地图上有很大用途,VML只是被IE支持. ...

  8. rem初始化

    使用css实现rem适配,头部引入即可 adaptive.js !function(e, t) { function i() { o = , e.devicePixelRatioValue = o, ...

  9. centos7更换镜像源

    更换软件源 由于国外的软件源在yum 安装时比较慢,更换为国内的源,以阿里的源的更换方式 下载wgetyum install wget -y echo 备份当前的yum源mv /etc/yum.rep ...

  10. 关于ip包长度

    http://blog.csdn.net/naturebe/article/details/6712153 这篇文章总结的不错,转自:http://hi.baidu.com/to_wait/blog/ ...