后缀自动机裸题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i<=k;--i)
#define inf 0x3f3f3f3f
#define ll long long
#define maxn 200005 struct sam{
char s[maxn];
int h[maxn],to[maxn],ne[maxn],en,ttt;
int dp[maxn][10],n;
int last,cnt,len,lth;
int go[maxn][26],l[maxn],fa[maxn];
void addedge(int a,int b)
{to[en]=b;ne[en]=h[a];h[a]=en++;}
void init()
{
memset(h,-1,sizeof h);en=0;
last=cnt=1;
// memset(go,0,sizeof go);
// memset(dp,0,sizeof dp);
}
void add(int x)
{
// printf("add %d\n",x);
int p=last,np=last=++cnt; l[np]=l[p]+1;
// printf("on point %d\n",np);
for (;p&&!go[p][x];p=fa[p]) go[p][x]=np;
if (!p) fa[np]=1;
else
{
int q=go[p][x];
if (l[q]==l[p]+1) fa[np]=q;
else
{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(go[nq],go[q],sizeof go[q]);
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
for (;p&&go[p][x]==q;p=fa[p]) go[p][x]=nq;
}
}
}
void dfs(int o)
{
for (int i=h[o];i>=0;i=ne[i])
{
dfs(to[i]);
F(j,1,ttt)
dp[o][j]=max(dp[o][j],dp[to[i]][j]);
}
}
void solve()
{
scanf("%d",&n);
scanf("%s",s+1);
lth=len=strlen(s+1);
F(i,1,lth) add(s[i]-'a');
int i=0;
while (scanf("%s",s+1)!=EOF)
{
++i;
// scanf("%s",s+1);
len=strlen(s+1);
int now=1,t=0;
F(j,1,len)
{
int x=s[j]-'a';
if (go[now][x]) {t++;now=go[now][x];}
else
{
while (now&&!go[now][x]) now=fa[now];
if (!now) {t=0;now=1;}
else t=l[now]+1,now=go[now][x];
}
dp[now][i]=max(dp[now][i],t);
// for (int k=now;k;k=fa[k]) dp[k][i]=max(dp[k][i],t);
}
}
dfs(1);
int ans=0;ttt=i;
F(i,1,cnt) addedge(fa[i],i);
F(i,1,cnt)
{
int tmp=l[i];
F(j,2,ttt)
tmp=min(tmp,dp[i][j]);
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
}SAM; int main()
{
SAM.init();
SAM.solve();
}

  

SPOJ LCS2 Longest Common Substring II ——后缀自动机的更多相关文章

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

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

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

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

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

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

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

  5. SPOJ LCS2 - Longest Common Substring II

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

  6. SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解

    题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...

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

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

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

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

  9. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

随机推荐

  1. ReactiveCocoa 响应式函数编程

    简介 ReactiveCocoa(简称为RAC),RAC具有函数响应式编程特性,由Matt Diephouse开源的一个应用于iOS和OS X的新框架. 为什么使用RAC? 因为RAC具有高聚合低耦合 ...

  2. Yslow使用方法

    Yslow是雅虎开发的基于网页性能分析浏览器插件,从年初我使用了YSlow后,改变了博客模板大量冗余代码,不仅提升了网页的打开速度,这款插件还帮助我分析了不少其他网站的代码,之前我还特意写了提高网站速 ...

  3. sql中的exsits和not exsits

    select * from table where exsits(sql语句) :  括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ...

  4. Redis学习笔记(一)五种数据类型

    1.字符串(String) 基本操作:SET(设置).GET(获取).DEL(删除)其他操作传送门 root@localhost:~# redis-cli > set msg hello OK ...

  5. OPENFIRE 接收数据流程图

    此图网上已经有,怎奈我不能上传大于10M的图片,所以截图了!各位请脑补!

  6. (十)mybatis之配置(mybatis-config.xml)

    配置  可以从前篇的文章中知道(https://www.cnblogs.com/NYfor2018/p/9093472.html ),要使用mybatis需要以下配置: 1.  mybatis-con ...

  7. POJ1077 八数码 BFS

    BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...

  8. javaEE(12)_数据库连接池

    一.直接获取数据库连接和通过池获取示意图: 二.编写数据库连接池 1.实现DataSource接口,并实现连接池功能的步骤: •在DataSource构造函数中批量创建与数据库的连接,并把创建的连接加 ...

  9. 第六次作业 :使用Excel制作成绩单

  10. vue-router介绍及简单使用

    一.vue-router介绍 vue-router是vue官方提供的一个路由框架,控制页面路由,使用较为方便. 1.路由模式 hash(浏览器环境默认值),使用 URL hash 值来作路由,支持所有 ...