传送门

后缀自动机基础题。

给出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. viewpager fragment 滑动界面

    先新建几个fragment,包括java和xml 然后在主界面的布局文件中: <android.support.v4.view.ViewPager android:id="@+id/m ...

  2. PHP20-challenge10

      今天咩,说一个关于php的题目,里面主要主要牵扯到截断的知识点,这让我多了解了一些机制. 1.截断   截断,简单来说就是16进制的00,代表空.其实,那些输出语句函数就是凭借语句后面这个我们看不 ...

  3. f5 SNMP配置

    1.选择监控终端 2.配置团体名称:

  4. TableView中Label自适应高度

    //Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableVi ...

  5. linux下Redis主从复制

    Redis的主从配置比起MySQL主从配置简单多了,而且Redis主从复制中一个主服务可以有多个从服务,一个从服务又可以有多个从服务. MySQL主从配置http://www.cnblogs.com/ ...

  6. Cloud Foundry v2 部署及入门运维

    之前写过一个Guide for Cloud Foundry New Teamer.不过似乎已经有些过时,那会实验室主要是针对的CF v1进行的研究,现在已经全面进入V2时代了.所以更新一下关于Clou ...

  7. ORACLE分组查询和统计等

    select flow_id,rw from (select t.flow_id ,rownum as rw from apex_030200.wwv_flow_list_templates t)  ...

  8. DevExpress之TreeList节点绑定图片

    最近在项目中使用到了DX中的TreeList控件绑定数据源时在每个节点前显示特点的图片:查阅相关资料实现方法如下:1.首先打开VS2010新建一个WINFROM应用程序: 2.在WINFROM应用程序 ...

  9. (转)Android EditText限制输入字符的5种实现方式

    最近项目要求限制密码输入的字符类型, 例如不能输入中文.   现在总结一下EditText的各种实现方式,  以比较各种方法的优劣. 第一种方式:  设置EditText的inputType属性,可以 ...

  10. js call方法的使用

    转自:js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg ...