【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. ubuntu14.04上实现faster rcnn_TF的demo程序及训练过程

    安装环境:Ubuntu14.04.显卡Tesla K40C+GeForce GT 705.tensorflow1.0.0.pycharm5.0 说明:原文见博客园,有问题原文下留言,不定期回复.本文作 ...

  2. dedecms在任意页面调用任意栏目文章

    dedecms在任意页面调用任意栏目文章,我们用arclist标签即可实现.如果是调用多个栏目文章可以给typeid多个值. 我们以调用ID为1和ID为30的两个栏目下5篇文章为例: {dede:ar ...

  3. NDK开发,如何配置 debug环境

    刚开始做NDK 开发的时候,Android Studio 还没提供了 native C/C++ 设置断点 调试,我们都是通过输出 日志来调试,这样费时耗力.Android Studio 应该是在 2. ...

  4. [记录]MySQL读写分离(Atlas和MySQL-proxy)

    MySQL读写分离(Atlas和MySQL-proxy) 一.阿里云使用Atlas从外网访问MySQL(RDS) (同样的方式修改配置文件可以实现代理也可以实现读写分离,具体看使用场景) 1.在跳板机 ...

  5. C#仪器数据文件解析-PDF文件

    不少仪器工作站输出的数据报告文件为PDF格式,PDF格式用于排版打印,但不易于数据解析,因此解析PDF数据需要首先读取到PDF文件中的文本内容,然后根据内容规则解析有意义的数据信息. C#解析PDF文 ...

  6. $.ajax的一些坑啊

    1.如果发送ajax返回的数据为json务必设置其 Content-Type:application/json;charset=UTF-8 不然会导致其success:function(data)中的 ...

  7. ios音乐播放器demo

    闲暇时间,写了一个音乐播放器. 个人认为,基于Demo 的学习是最有效果的. 想学习的同学,欢迎下载.知识,只有在传播的时候才有价值. 不懂之处,欢迎留言询问,将热情解答. 运行图 项目结构图 Git ...

  8. 老男孩Python全栈开发(92天全)视频教程 自学笔记21

    day21课程内容:  json: #序列化 把对象(变量)从内存中 编程可存储和可传输的过程 称为序列化import jsondic={'name':'abc','age':18}with open ...

  9. leetcode第一天

    leetcode 第一天 2017年12月24日 第一次刷leetcode真的是好慢啊,三道题用了三个小时,而且都是简单题. 数组 1.(674)Longest Continuous Increasi ...

  10. 《android开发艺术探索》读书笔记(九)--四大组件

    接上篇<android开发艺术探索>读书笔记(八)--WindowManager No1: 四大组件除了BroadcastReceiver,其他三种组件都必须在AndroidManifes ...