【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. 详解CSS display:inline-block的应用

    阅读目录 基础知识 inline-block的问题 inline-block的应用 总结 本文详细描述了display:inline-block的基础知识,产生的问题和解决方法以及其常见的应用场景,加 ...

  2. centos7设置静态ip

    动态ip可以上网.静态ip设置成功后,发现不能上网. 1.首先查看动态ip的默认网关 cat /etc/resolv.conf 2.设置配置文件 在 /etc/sysconfig/network-sc ...

  3. Git hook实现自动部署

    Git Hook 是 Git 提供的一个钩子,能被特定的事件触发后调用.其实,更通俗的讲,当你设置了 Git Hook 后,只要你的远程仓库收到一次 push 之后,Git Hook 就能帮你执行一次 ...

  4. 使用wireshark抓取TCP包分析1

    使用wireshark抓取TCP包分析1 前言 介绍 目的 准备工作 传输 创建连接 握手 生成密钥 发送数据 断开连接 结论 前言 介绍 本篇文章是使用wireshrak对某个https请求的tcp ...

  5. Angular4.0用命令行创建组件服务出错

    之前使用cnpm创建的angular4.0项目,由于cnpm下载的node_modules资源经常会有部分缺失,所以在用命令行创建模板.服务的时候会报错: Error: ELOOP: too many ...

  6. JVM中对象访问定位两种方式

    1.通过句柄方式访问, 在Java堆中分出一块内存进行存储句柄池,这样的话,在栈中存储的是句柄的地址 优点: 当对象移动的时候(垃圾回收的时候移动很普遍),这样值需要改变句柄中的指针,但是栈中的指针不 ...

  7. centos/linux下的安装Tomcat

    1.启动tomcat时候需要JDK依赖 如果没有安装的请移步到该链接Centos/linux下的JDK安装 2.从官网上下载tomcat压缩包 wget -c http://apache.fayea. ...

  8. org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Users/lonecloud/tomcat/apache-tomcat-7.0.70 2/webapps/myproject/WEB-INF/classes/cn/lone

    解决这个报错的解决办法: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidat ...

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

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

  10. 第十八章 DjangoWeb开发框架

    第十八章 DjangoWeb开发框架 第一课 内容概要: 1.JS正则 -登录注册验证 2.组件 1.BootStrap -css -js 学习BootStrap规则 2.jQueryUI -css ...