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 ...
随机推荐
- WCF引用 代码
方法1: using (ChannelFactory<ICommonService> channelFactory = new ChannelFactory<ICommonServi ...
- 模块 - time/datetime
time 模块 time模块方法: >>> import time >>> time.time() #时间戳 秒级别 1519212085.6211221 #从19 ...
- 删除DOM元素 parent.removeChild(target)
p.removeChild(p.children[0]);
- PyMongo的使用(转)
原文:http://www.oschina.net/code/snippet_1382328_37407 #!/usr/bin/env python #coding:utf-8 # Author: - ...
- mysql 如何找出两张表之间的关系
分析步骤: #1.先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id) #2.再站在右表的角度去找 是否右表的 ...
- 初识ganglia
本文地址:http://www.cnblogs.com/qiaoyihang/ 一.Ganglia是什么?Ganglia主要用来解决什么样的问题? ganglia是一个可扩展的分布式监控系统,用于监控 ...
- HTML,CSS,Javascript,JQuery
HTML 一套浏览器认识的规则 标签 1.<head></head> 2.<title></title> 3.<body></body ...
- unity, 在编辑界面中隐藏公开变量
unity默认声明为public的变量都是在编辑界面可见的,如果要隐藏的话就可以这样做 一种是使用属性 Public float Age { get; set; } 另一种是使用标签 [HideInI ...
- MySQL SQL Injection(注入)
如果通过网页接收用户输入,而后再把这些数据插入到数据库中,那么你可能就会碰到 SQL 注入式攻击.本节简要介绍如何防范这种攻击,确保脚本和 MySQL 语句的安全性. 注入式攻击往往发生在要求用户输入 ...
- python 利用PIL库进行更改图片大小的操作
python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需 ...