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)\),求这些字符串的最长公共子 ...
 
随机推荐
- 关于MySQL数据库的备份方案
			
这里简单总结MySQL的备份分为3种:分为冷备份,逻辑备份,热备份. 1.冷备份: 一般主要用于非核心业务,这类业务一般都是允许业务中断的,冷备份的特点就是数度快,恢复时也最为简单.通常直接复物理文件 ...
 - tight
			
tight - 必应词典 美[taɪt]英[taɪt] adv.紧紧地:牢固地 adj.牢固的:紧的:不松动的:难解开的 n.紧身衣 网络紧身的:紧密的:密封的 变形比较级:tighter:最高级:t ...
 - RxJS之catchError
			
Catches errors on the observable to be handled by returning a new observable or throwing an error. 返 ...
 - 前端框架(kraken、Express、Node、MVC)
			
You know my loneliness is only kept for you, my sweet songs are only sang for you. 前端框架相关知识记录. krake ...
 - git add和git commit
			
git命令使用:提交前可指定要提交哪些文件,然后使用git commit来提交 样例: git status 输出: Changes to be committed: modified: app/ ...
 - python第三方库requests简单介绍
			
一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r ...
 - export命令
			
http://blog.csdn.net/wl_fln/article/details/7258294 http://man.linuxde.net/export export命令 功能说明:设置或显 ...
 - 非线性优化(高翔slam---第六讲 )
			
1.线性最小二乘问题 2.非线性最小二乘问题 因为它非线性,所以df/dx有时候不好求,那么可以采用迭代法(有极值的话,那么它收敛,一步步逼近): 这样求导问题就变成了递归逼近问题,那么增量△xk如何 ...
 - 【gRPC使用问题3】生成出来无法识别Google.Api.AnnotationsReflection.Descriptor
			
1.问题截图: 2.解决方案: Install the package "Google.Api.Gax.Grpc". From the Package Manager Consol ...
 - 【教程】教你解决“Windows 资源保护找到了损坏文件但无法修复其中某些文件”的问题【转载】
			
转载:http://www.cystc.org/?p=2827 很多人都会用sfc /scannow来解决系统文件损坏的问题,但有时也会遇到连sfc都无法修复的情况,最常见的就是出现“Windows ...