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

题面

Vjudge

题意:求两个串的最长公共子串

题解

\(SA\)的做法很简单

不再赘述

对于一个串构建\(SAM\)

另外一个串在\(SAM\)上不断匹配

最后计算答案就好了

匹配方法:

如果\(trans(s,c)\)存在

直接沿着\(trans\)走就行,同时\(cnt++\)

否则沿着\(parent\)往上跳

如果存在\(trans(now,c),cnt=now.longest+1\)

否则,如果不存在可行的\(now\),

\(now=1\)也就是空串所在的节点,\(cnt=0\)

#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 255000
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
int last=1,tot=1;
char ch[MAX];
int ans;
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;
}
}
}
int main()
{
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-97);
scanf("%s",ch+1);
for(int i=1,l=strlen(ch+1),now=1,tt=0;i<=l;++i)
{
if(t[now].son[ch[i]-97])
++tt,now=t[now].son[ch[i]-97];
else
{
while(now&&!t[now].son[ch[i]-97])now=t[now].ff;
if(!now)tt=0,now=1;
else tt=t[now].len+1,now=t[now].son[ch[i]-97];
}
ans=max(ans,tt);
}
printf("%d\n",ans);
return 0;
}

【SPOJ】Longest Common Substring(后缀自动机)的更多相关文章

  1. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

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

  2. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  3. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

  4. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  5. spoj - Longest Common Substring(后缀自动机模板题)

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

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

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

  7. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  8. spoj Longest Common Substring

    Longest Common Substring SPOJ - LCS 题意:求两个串的最长公共子串 /* 对一个串建立后缀自动机,用另一个串上去匹配 */ #include<iostream& ...

  9. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

  10. SPOJ Longest Common Substring II

    题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...

随机推荐

  1. 在linux内核中实现自己的系统调用

    如实现一个简单的打印:printk 1.cd linux-ok6410/kernel/ vim printk.cvoid sys_pk(){printk("<0>this is ...

  2. 【css3】旋转倒计时

    很多答题的H5界面上有旋转倒计时的效果,一个不断旋转减少的动画,类似于下图的这样. 今天研究了下,可以通过border旋转得到.一般我们可以通过border得到一个四段圆. See the Pen c ...

  3. 观察者模式—jdk自带源码分析

    一:观察者模式简介 二:jdk实现观察者模式的源码 三:实际例子 四:观察者模式的优点和不足 五:总结 一:观察者模式简介 有时又被称为发布(publish )-订阅(Subscribe)模式.模型- ...

  4. Python构建发布

    click python配置apache的web服务器方法(python的CGI配置) python中的编码问题 http://blog.csdn.net/wyb_hardworking/articl ...

  5. Linux系统Go开发环境搭建

    Go 语言是由谷歌的科学家开发的,并开源的新语言,被誉为"21世纪的C语言",它的主要目标是将静态语言的安全性和高效性与动态语言的易开发性进行有机结合,达到完美平衡,从而使编程变得 ...

  6. Oracle,Sql,procedure 感觉自己写的很棒的一个存储过程

    感觉自己写的很棒的一个Oracle存储过程,(其实想说很叼^,^). 集成了一堆操作数据的功能(至少几十), 包括存储过程执行异常信息输出帮助诊断. 亮点很多, 比如`over(partition b ...

  7. Python报错:IndentationError: expected an indented block

    sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print(sum) 代码如上,但是运行报错: 发现是因为少了缩进,改正 ...

  8. eclipse 使 用Ctrl+鼠标左键进入mapper.xml文件的方法

    在 >eclipse MarketPlace中下载>Mybatipse 插件安装重启即可完成

  9. 用户不在sudoers 文件中。此事将被报告 or (usermod:“sudo”组不存在)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 异常处理汇总-服 务 器 http://www.cnblogs.com/dun ...

  10. Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...