后缀自动机板子题

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的更多相关文章

  1. 【spoj LCS2】 Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...

  2. SPOJ LCS2 - Longest Common Substring II 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...

  3. SPOJ LCS2 - Longest Common Substring II

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

  4. Virtual Judge SPOJ - LCS2 Longest Common Substring II

    https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...

  5. 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 ...

  6. SPOJ LCS2 后缀自动机

    多串的LCS,注意要利用拓扑序更新suf的len. 我用min,max,三目会超时,所以都改成了if,else #pragma warning(disable:4996) #include<cs ...

  7. 【SPOJ - LCS2】Longest Common Substring II【SAM】

    题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...

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

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

  9. 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 ...

随机推荐

  1. Windows数据库定时备份

    首先打开:任务计划程序 右键任务计划程序库,选择创建基本任务 然后即可以按照实际情况逐步进行 直到启动程序--浏览(程序或脚本)时,这里本人导入的是backup.bat文件,文件内容为 @echo 设 ...

  2. 编译安装cmake3

    编译安装cmake3 ubuntu 14 的系统默认安装的是cmake2.7,apt-get upgrade之后也还是cmake2.7,而很多软件如今需要3及以上的版本来进行cmake编译(如caff ...

  3. javascript变量声明提升和函数声明提升

    在ES6之前,JavaScript没有块级作用域(一对花括号{}即为一个块级作用域),只有全局作用域和函数作用域.变量提升即将变量声明提升到它所在作用域的最开始的部分. JS的解析过程分为两个阶段:预 ...

  4. git 解决push报错:[rejected] master -> master (fetch first) error: failed to push some refs to

    今天对代码进行了修改优化,然后往往远程push,但push后报错了 git操作 git add . git commit -m"fix" git push origin maste ...

  5. 【我的Android进阶之旅】如何快速寻找Android第三方开源库在Jcenter上的最新版本

    问题描述 解决方法 先了解compile comsquareupokhttpokhttp240的意义 了解Jcenter和Maven jcenter Maven Central 理解jcenter和M ...

  6. PHP Laravel 本地化语言支持

        That`s it. 我发如今网上Laravel的学习资料实在是太少了.好多东西须要自己去弄.去理解. 我的方法另一个,就是去github上面下载老外写的Laravel站点,然后拿下来自己执行 ...

  7. redis的图形化工具(四)

    1. 介绍 本篇会介绍几个关于redis的图形化的监控工具和管理工具. 2. redis-stat redis-stat提供终端和web端的监控页面,它安装和使用起来很简单. 安装只需要一条指令. $ ...

  8. Redis三(List操作)

    List操作 redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush(name,values) 1 2 3 4 5 6 7 8 # 在name对应的list中添加元 ...

  9. str()和repr()(以及``操作符)

    内建函数str()和repr()或反引号操作符(``)可以方便的获取字符串. str它会把值转换为合理形式的字符串,可读性更好,同时对用户来说更加友好. repr它会创建一个字符串,以合法的Pytho ...

  10. C# 复杂算法

    1.添加命名空间引用using Microsoft.JScript; //formula 是公式如:(1+2)*3/10 private double GetComputeValueByStringF ...