【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 (后缀自动机)的更多相关文章

  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 - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

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

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

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

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

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

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

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

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

  7. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

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

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

  9. spoj1812-Longest Common Substring II(后缀自动机)

    Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...

  10. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

随机推荐

  1. 【JavaWeb】DbUtils入门之QueryRunner

    DbUtils简介 根据官网的介绍,DbUtils是一种 JDBC Utility Component (翻译过来大概就是:JDBC实用部件),故名思意,和数据库操作有关 官网上的简介也称之为 JDB ...

  2. 基于Centos7的autobahn-python+crossbar的环境搭建

    一.基于centos7的crossbar安装(已经安装好python) (1) sudo yum update (2) sudo yum install gcc gcc-c++ make openss ...

  3. ASP.NET与ASP.NET MVC 的差异、优点及缺点

    众所周知,在微软的编程语言发展历史中,asp.net是不得不提的一个重要的发展阶段,它具有快速开发.层级明确的优点,但最大的缺点,同时也是它逐渐被废弃的原因就是,页面加载的viewstate过于庞大, ...

  4. 了解一下Http常见状态码、Http协议的工作特点和原理、Http请求Post与Get的区别

    HTTP协议常见状态码状态码的作用负责标记客户端请求服务器的返回结果,标记服务器端的处理是否正常,通知出现的错误等等职责,借助客户端可以知道客户端是否正常请求服务端.五大类:1XX(信息类状态码,接收 ...

  5. 在windows上安装nginx

    在windows上安装nginx   最近自己也尝试了一下在windows上安装nginx,其实非常的简单,这里算是备忘一下.   首先需要到nginx的官网上下载最新版的nginx:http://n ...

  6. hihoCoder 403 Forbidden 字典树

    题意:给定个规则,个ip,问这些ip是否能和某个规则匹配,如果有多个规则,则匹配第一个.如果没能匹配成功,则认为是"allow",否则根据规则决定是"allow" ...

  7. yaf框架刚开始遇到的问题

    2016-10-17 17:54:13遇到的这个问题,这个问题算是比较综合性的问题,我也是查阅了很多的资料才大概明白的.这里就简单记录一下: 1.首先查看日志记录,结果如下: 根据错误日志:找寻到 ( ...

  8. Unix代码段和数据段

    关于UNIX系统代码段和数据段分开的目的:方便编程. 1)代码段:代码段是用来存放可执行文件的操作指令,也就是说是它是可执行程序在内存中的镜像.代码段需要防止在运行时被非法修改,所以只准许读取操作,而 ...

  9. keepalived双机热备nginx

    nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽 ...

  10. linux查看端口被占用等常用命令

    一   根据端口号 查找对应的服务 比如我们查查找端口号8189对应的服务是哪个 1  先根据端口号查找对应对的pid(进程id)为23367 netstat -anp  | grep 8189    ...