传送门

后缀自动机基础题。

给出10个串求最长公共子串。


我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
const int N=2e5+5;
int T=0,n;
char s[N];
struct SAM{
	int rt,tot,last,len[N],lz[N],cnt[N],rk[N],val[N],link[N],son[N][26],mn[N],mx[N];
	SAM(){rt=tot=last=1,len[0]=-1,fill(son[0],son[0]+26,1);}
	inline void expend(int x){
		int p=last,np=++tot;
		last=np,len[np]=len[p]+1;
		while(p&&!son[p][x])son[p][x]=np,p=link[p];
		if(!p){link[np]=rt;return;}
		int q=son[p][x],nq;
		if(len[q]==len[p]+1){link[np]=q;return;}
		len[nq=++tot]=len[p]+1,memcpy(son[nq],son[q],sizeof(son[q])),link[nq]=link[q];
		while(p&&son[p][x]==q)son[p][x]=nq,p=link[p];
		link[np]=link[q]=nq;
	}
	inline void topsort(){
		for(ri i=1;i<=tot;++i)mn[i]=len[i];
		for(ri i=1;i<=tot;++i)++cnt[len[i]];
		for(ri i=1;i<=last;++i)cnt[i]+=cnt[i-1];
		for(ri i=1;i<=tot;++i)rk[cnt[len[i]]--]=i;
	}
	inline void update(){
		int p=1,nowlen=0;
		for(ri i=1;i<=tot;++i)mx[i]=0;
		for(ri i=1,x;i<=n;++i){
			x=s[i]-'a';
			if(son[p][x])p=son[p][x],++nowlen;
			else{
				while(!son[p][x])p=link[p];
				nowlen=len[p]+1,p=son[p][x];
			}
			mx[p]=max(mx[p],nowlen);
		}
		for(ri p,i=tot;i;--i)p=rk[i],mn[p]=min(mn[p],mx[p]),mx[link[p]]=min(len[link[p]],max(mx[link[p]],mx[p]));
	}
	inline void query(){
		int ans=0;
		for(ri i=1;i<=tot;++i)ans=max(ans,mn[i]);
		if(ans<2)ans=0;
		cout<<ans;
	}
}sam;
int main(){
	while(~scanf("%s",s+1)){
		++T,n=strlen(s+1);
		if(T==1){for(ri i=1;i<=n;++i)sam.expend(s[i]-'a');sam.topsort();}
		else sam.update();
	}
	sam.query();
	return 0;
}

2018.12.15 spoj Longest Common Substring II(后缀自动机)的更多相关文章

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

    传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...

  2. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...

  3. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

  4. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  5. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

  6. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  7. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  8. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  9. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

随机推荐

  1. 207. Course Schedule(Graph; BFS)

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  2. vue 实现多选

    v-model <template> <!--用户页面-选择关注--> <div class="follow"> <h4>选择关注& ...

  3. 利用jenkins+saltstack+sh部署项目到多台服务器

    jenkins的配置(这里作用只是当做界面使用,利用它来管理执行salt命令,这里以 shop.51ekt.com 这个项目目录为例) 1.利用参数化构建项目,来实现是发布还是回滚操作: 2.构建操作 ...

  4. ApplicationContextAware的使用

    一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...

  5. javascript中的类型转换(进制转换|位运算)

    1:parseInt(string) : 这个函数的功能是从string的开头开始解析,返回一个整数 parseInt("123hua"); //输出 123 parseInt(& ...

  6. 梦殇 chapter one

    梦殇 chapter one 星梦 天空中飘着几片云,喝着小鸟的欢呼声,这一切似乎显得愈加可爱了. 不觉间已经到了2013年,错过的12年,似乎在向我们招手,不知道远方的朋友们,你们还好吗? 是否也会 ...

  7. better-scroll使用总结

    参考:https://zhuanlan.zhihu.com/p/27407024 better-scroll使用小结 核心就是这4个 <script> import BScroll fro ...

  8. c#devexpress GridContorl datasource为 类字段的实现方式 非datatable方式以及其他操作总结

    1:定义model class A { public string a{get;set;} public string b{get;set;} } 2:赋值: A aobj=new A(); aobj ...

  9. 《大道至简》第一章--编程的精意 读后感(JAVA伪代码)

    1. /*愚公移山 原始需求:惩山北之塞,出入之迂: 项目沟通:聚室而谋曰: 项目目标:毕力平险,指通豫南,达于汉阴: 技术方案:扣石垦壤,箕畚运于渤海之尾: 人员构成:愚公率子孙荷担者三夫,邻人京城 ...

  10. AsyncTask的缺陷

    导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验.在越来越讲究用户体验的大环境下,用户也许会因为应用的 ...