【SPOJ】Longest Common Substring II (后缀自动机)
【SPOJ】Longest Common Substring II (后缀自动机)
题面
Vjudge
题意:求若干个串的最长公共子串
题解
对于某一个串构建\(SAM\)
每个串依次进行匹配
同时记录\(f[i]\)表示走到了\(i\)节点
能够匹配上的最长公共子串的长度
当然,每个串的\(f[i]\)可以更新\(f[i.parent]\)
所以需要拓扑排序
对于每个串求出每个节点的最长匹配
然后对他们取\(min\),表示某个节点大家都能匹配的最长长度
最后对于所有点的值都取个\(max\)就是答案
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 120000
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
char ch[MAX];
int sum[MAX<<1],ans[MAX<<1];
int last=1,tot=1;
int c[MAX<<1],p[MAX<<1];
void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-97);
for(int i=1;i<=tot;++i)c[t[i].len]++;
for(int i=1;i<=tot;++i)c[i]+=c[i-1];
for(int i=1;i<=tot;++i)p[c[t[i].len]--]=i;
for(int i=1;i<=tot;++i)ans[i]=t[i].len;
while(scanf("%s",ch+1)!=EOF)
{
memset(sum,0,sizeof(sum));
for(int i=1,l=strlen(ch+1),now=1,tt=0;i<=l;++i)
{
int c=ch[i]-97;
if(t[now].son[c])++tt,now=t[now].son[c];
else
{
while(now&&!t[now].son[c])now=t[now].ff;
if(!now)tt=0,now=1;
else tt=t[now].len+1,now=t[now].son[c];
}
sum[now]=max(sum[now],tt);
}
for(int i=tot;i;--i)sum[t[p[i]].ff]=max(sum[t[p[i]].ff],sum[p[i]]);
for(int i=1;i<=tot;++i)ans[i]=min(ans[i],sum[i]);
}
int Ans=0;
for(int i=1;i<=tot;++i)Ans=max(Ans,ans[i]);
printf("%d\n",Ans);
return 0;
}
【SPOJ】Longest Common Substring II (后缀自动机)的更多相关文章
- 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 - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- spoj1812-Longest Common Substring II(后缀自动机)
Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
随机推荐
- ubuntu下smb的配置
PS: 转自Ubuntu中文论坛 -------------------------------------------------------------------------------- ...
- 读书共享 Primer Plus C-part11
第十四章结构和其他数据形式 关于fread以及fwrite fread(char* buff,int size,int count,FILE* fp) fwrite(char* buff,int si ...
- SpringBoot入门Demo
前言:相信做java后台编程的童鞋都知道Spring家族,Spring作为我们项目中必备的框架.JavaSpringBoot号称javaEE的颠覆者,这引起了本Y的好奇,这才花费了一点时间,学习了下s ...
- 整理的linux面试运维题
如何在非交互模式下把 /home/example/下所有.conf文件中的 192.168.0.2 改成 db01 ? find /home/example/ -type f -name &quo ...
- 洛谷P2286 [HNOI2004]宠物收养场【Treap】题解+AC代码
题目传送门啦~啦~啦~ 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...
- 微信小程序AES加密解密
微信小程序 其实在调用一些第三方 js的时候 其实没有大家想的那么复杂,无非就是把原生js调用方式 改为微信小程序 js调用方式 废话不多说直接贴代码 其实就是将原生function 或者 对象 ...
- WPF&Winform版本地图引擎
最近几年一直从事地图方面的工作,自主研发了WPF和Winform两个版本瓦片地图引擎.轻量级.不依赖第三库.先上一张图片展示一下吧! 产品包括服务端和客户端两部份: 1.服务端主要地图图层配制和空间计 ...
- MysqL应该考虑到的安全策略
1:使用预处理语句防止SQL注入2:写入数据库的数据要进行特殊字符的转义,比如字符中带单引号和双引号需要在应用层转义,这样为了防止SQL注入3:查询的错误信息不要返回给用户,将错误记录到日志.错误信息 ...
- 在SpringBoot使用Druid进行数据监控
前言 之前在构建项目初始设计的时候在选择数据库连接的时候就看到Druid有这样的强大的功能.数据监控.对于一个项目来说,数据监控特别重要,之前使用对于数据库的监控都是通过mysql的日志等系统来完成的 ...
- DOCKER 无法获取使用宿主机DNS 的原因,解决方法
今天在公司服务器上部署项目,遇到一个大坑.接口怎么请求都不同,宿主机DNS已经改了.宿主可以请求,找了半天,原来是DOCKER 没有获取到主机的DNS 进去DOCKER解析不了域名 指定DNS 启动也 ...