【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. react-native WebView 返回处理 (非回调方法可解决)

    1.前言 项目中有些页面内容是变更比较频繁的,这些页面我们会考虑用网页来解决. 在RN项目中提供一个公用的Web页,如果是网页内容,就跳转到这个界面展示. 此时会有一个问题是,网页会有一级页面,二级页 ...

  2. 洛谷P2756飞行员配对方案问题 P2055假期的宿舍【二分图匹配】题解+代码

    洛谷 P2756飞行员配对方案问题 P2055假期的宿舍[二分图匹配] 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架 ...

  3. SqlBulkCopy 参数配置示例

    SqlBulkCopy  做为SQL Server 官方 批量入库类,性能不会太差.针对其参数做了一些测试.   A. 先准备测试场景 ,关于SqlBulkCopyOptions.KeepIdenti ...

  4. es2015及es2017对我们的编程方式造成了什么影响?

    记一些写代码中用得到的es6+语法,至于什么正则的拓展,数组的什么fill方法,对我们来说用处不大,就不提及了. 还有es6的import模块和class模块,这些在各种框架中都有体现,而且语法简单, ...

  5. POJ - 1860 Bellman-Ford判正环

    心累,陕西邀请赛学校不支持,可能要自费了.. 思路:套用Bellman-Ford判断负环的思路,把大于改成小于即可判定是否存在从源点能到达的正环.如果存在正环,那么完全多跑几次正环就可以把钱增加到足够 ...

  6. 情景linux--如何优雅地退出telnet

    情景linux--在脚本中如何优雅地退出telnet 情景 telnet命令是TELNET协议的用户接口,它支持两种模式:命令模式和会话模式.虽然telnet支持许多命令,但大部分情况下,我们只是使用 ...

  7. 基于 HTML5 Canvas 的交互式地铁线路图

    前言 前两天在 echarts 上寻找灵感的时候,看到了很多有关地图类似的例子,地图定位等等,但是好像就是没有地铁线路图,就自己花了一些时间捣鼓出来了这个交互式地铁线路图的 Demo,地铁线路上的点是 ...

  8. python更新数据库脚本两种方法

    最近项目的两次版本迭代中,根据业务需求的变化,需要对数据库进行更新,两次分别使用了不同的方式进行更新. 第一种:使用python的MySQLdb模块利用原生的sql语句进行更新 import MySQ ...

  9. VC6安装错误——Error Launching acmboot.exe

    因项目需要,我需要安装Microsoft Visual C++ Professional Version 6 SP5.但是在安装时运行安装目录下的setup.exe,出现Error Launching ...

  10. 第3章 PCI总线的数据交换

    PCI Agent设备之间,以及HOST处理器和PCI Agent设备之间可以使用存储器读写和I/O读写等总线事务进行数据传送.在大多数情况下,PCI桥不直接与PCI设备或者HOST主桥进行数据交换, ...