【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. ...
随机推荐
- [Python Study Notes] Basic I\O + File 操作
列表操作 Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...
- 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法
主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...
- IOS设备设计完整指南
作为初学者,常常不知如何下手设计,IOS应用UI设计中碰到的种种基础小问题,在此都将一一得到解答.这份完整的设计指南将带你快速上手,为IOS设计出优雅的应用吧. 关于此设计指南 此设计指南描述的是如何 ...
- PHP Extension开发(Zephir版本)
上篇介绍了C语言开发PHP扩展的方法, 现在介绍使用Zephir开发扩展的方法. 关于Zephir需要简单介绍一下: Zephir 是为PHP开发人员提供的能够编写可编译/静态类型的高级语言.是优秀的 ...
- C# 使用 SmtpClient 发送邮件注意项
最近有邮件发送需求,使用 C# SmtpClient 对象发送邮件 , 报异常, 如下错误代码: 调整代码顺序后,发送邮件成功! 注意:一定要先设置 EnableSsl和UseDefaultCred ...
- Java导出freemarker实现下载word文档格式功能
首先呢,先说一下制作freemarker模板步骤, 1. 在WPS上写出所要的下载的word格式当做模板 2. 把模板内不固定的内容(例:从数据库读取的信息)写成123或者好代替的文字标注 3. 把固 ...
- Log4j与Log4j2
完整的软件,日志是必不可少的.程序从开发.测试.维护.运行等环节,都需要向控制台或文件等位置输出大量信息.这些信息的输出,在很多时候是System.out.println()无法完成日志信息根据用途与 ...
- Ceph神坑系列
1. 在使用librbd的过程中,发现一个如果ceph集群异常librbd函数不返回的问题,对librbd的源码进行跟踪,发现了rados接口中的rados_mon_op_timeout(how ma ...
- 原创:实现ehcache动态创建cache,以及超期判断的具体逻辑
当前最常用的三个缓存组件:ehcache.redis.memcached 其中,ehcache与应用共同运行于JVM中,属于嵌入式组件,运行效率最高,因此常被用于实现一级缓存. 在更复杂的一些系统中, ...
- 转载 git Unknown SSL protocol error in connection to github.com:443
1.执行命令:git pull –progress –no-rebase -v "origin",报错,如图1 fatal: unable to access 'https://g ...