传送门

后缀自动机模板题。

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


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

代码:

#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. 唯品会海量实时OLAP分析技术升级之路

    本文转载自公众号 DBAplus社群 , 作者:谢麟炯 谢麟炯,唯品会大数据平台高级技术架构经理,主要负责大数据自助多维分析平台,离线数据开发平台及分析引擎团队的开发和管理工作,加入唯品会以来还曾负责 ...

  2. awk参数解析

    # awk --help Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or ...

  3. [z]c++ 和 java 利用protobuf 通讯

    [z]http://andinker.iteye.com/blog/1979428  java端的具体步骤如下: 1.首先下载 下载protobuf 编译工具   http://code.google ...

  4. sql:inner join,left join,right join,full join用法及区别

    join的语法: select [字段] from [表名1] inner/left/right/full join [表名2] on [表名1.字段1] <关系运算符> [表名2.字段2 ...

  5. 金老师的经典著作《一个普通IT人的十年回顾》

    学习人生             -------一个普通IT人的十年回顾(上)序从1994到2003,不知不觉之间,我已在计算机技术的世界里沉浸了十年.有位哲人说过:如果一个人能用十年的时间专心致志地 ...

  6. C# 通过api函数GetPrivateProfileString读取ini文件,取不到值

    通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...

  7. andorid 列表视图 ListView 之ArrayAdapter

    activity_ui3.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns ...

  8. Win10传递优化设置技巧

    什么是“传递优化缓存” “传递优化”是微软为了加快Windows更新和Microsoft Store应用更新的下载速度,而在Windows10中引入的一种“自组织分布式本地化缓存”设计,可以在用户电脑 ...

  9. python下的MySQL数据库编程

    https://www.tutorialspoint.com/python/python_database_access.htm if you need to access an Oracle dat ...

  10. CH6901 骑士放置

    原题链接 和棋盘覆盖(题解)差不多.. 同样对格子染色,显然日字的对角格子是不同色,直接在对应节点连边,然后就是二分图最大独立集问题. #include<cstdio> #include& ...