SPOJ - LCS2
后缀自动机板子题
https://vjudge.net/problem/28017/origin
找多串的最长公共子串
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod (1000000007)
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; char s[N];
int c[N],a[N];
struct SAM{
int last,cnt;
int ch[N<<][],fa[N<<],l[N<<];
int mx[N<<],tmp[N<<];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=;
else
{
int q=ch[p][c];
if(l[p]+==l[q])fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
}
void build(){
int len=strlen(s+);
last=cnt=;
for(int i=;i<=len;i++)ins(s[i]-'a');
topo();
for(int i=;i<=cnt;i++)mx[i]=l[i],tmp[i]=;
}
void topo(){
for(int i=;i<=cnt;i++)c[l[i]]++;
for(int i=;i<=cnt;i++)c[i]+=c[i-];
for(int i=;i<=cnt;i++)a[c[l[i]]--]=i;
}
void debug()
{
puts("");
for(int i=;i<=cnt;i++)
{
printf("%d ",i);
for(int j=;j<;j++)
printf("%d ",ch[i][j]);
printf("%d %d\n",fa[i],l[i]);
}
}
void faupdate()
{
for(int i=cnt;i;i--)
tmp[fa[a[i]]]=max(tmp[fa[a[i]]],tmp[a[i]]);
}
void maupdate()
{
for(int i=;i<=cnt;i++)
mx[i]=min(mx[i],tmp[i]),tmp[i]=;
}
void match()
{
int len=strlen(s+),val=,now=,res=;
for(int i=;i<=len;i++)
{
if(ch[now][s[i]-'a'])
{
now=ch[now][s[i]-'a'];
res++;
}
else
{
while(now&&!ch[now][s[i]-'a'])now=fa[now];
if(!now)now=,res=;
else
{
res=l[now]+;
now=ch[now][s[i]-'a'];
}
}
tmp[now]=max(tmp[now],res);
}
faupdate();
maupdate();
}
int maxlen()
{
int ans=;
for(int i=;i<=cnt;i++)ans=max(ans,mx[i]);
return ans;
}
}sam;
int main()
{
scanf("%s",s+);
sam.build();
while(~scanf("%s",s+))
sam.match();//,printf("%d\n",sam.maxlen());
printf("%d\n",sam.maxlen());
return ;
} /******************** ********************/
SPOJ - LCS2的更多相关文章
- 【spoj LCS2】 Longest Common Substring II
http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- Virtual Judge SPOJ - LCS2 Longest Common Substring II
https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...
- 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 ...
- SPOJ LCS2 后缀自动机
多串的LCS,注意要利用拓扑序更新suf的len. 我用min,max,三目会超时,所以都改成了if,else #pragma warning(disable:4996) #include<cs ...
- 【SPOJ - LCS2】Longest Common Substring II【SAM】
题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】
多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...
随机推荐
- gerrit添加appkey以及简单添加分支
最近团队开放用上gerrit版本项目管理工具,简单说一下appkey配置过程 首先是拿到gerrit分配的账户密码.然后进入到首页,假如是新搭建的应该是没有信息,我这里有一些提交的信息,然后找到右上角 ...
- Java 语言基础之运算符
使用运算符之后,肯定有返回结果. 六种运算符: 算术运算符 赋值运算符 比较运算符 逻辑运算符 位运算符 三元运算符 1. 算术运算符 加(+), 减(-), 乘(*), 除(/), 取余(%), 自 ...
- 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作
/** * 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作 */ public class ThreadTest { private int j; public static vo ...
- unity里无损实现制定物体bloom
虽然标题是实现hdr+bloom,但实际感觉更像是控制单个物体bloom https://blog.csdn.net/zsy654321/article/details/80682651
- form表单学习
1.http://www.cnblogs.com/fish-li/archive/2011/07/17/2108884.html 2.http://www.cnblogs.com/polk6/arch ...
- 服务系统 server端
from django.shortcuts import render,HttpResponse import json # Create your views here. from reposito ...
- 010-shiro与spring web项目整合【四】缓存Ehcache
一.Ehcache shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合. 1.添加Ehc ...
- 编译原理课后习题答案令A,B和C是任意正规式,证明以下关系成立(A|B)*=(A*B*)*=(A*|B*)*
题目: 令A.B和C是任意正规式,证明以下关系成立: A∣A=A (A*)*= A* A*=ε∣A A* (AB)*A=A(BA)* (A∣B)*=(A*B ...
- mongo常用查询
复杂查询: and: or: lte,gte,=: and+lt: , 逗号表示and, $lt小于写在值当中 查询实例: 找到含有指定数据文档 查找条件spcode有1个字符长度的文档 db.sp ...
- NHibernate & INotifyPropertyChanged
One of the things that make NHibernate easy to use is that it fully support the POCO model. But one ...