【SPOJ】Longest Common Substring

求两个字符串的最长公共子串

对一个串建好后缀自动机然后暴力跑一下 废话

讲一下怎么跑吧

从第一个字符开始遍历,遍历不到了再沿着\(parents\)走看能否找到出路,走到某个点时,统计一下走过了多少点然后更新答案

来说说这样做的正确性:

遍历是肯定的, SAM 从根节点出发的任意路径都表示一个子串

沿着\(parents\)边往后走,保证贪心情况下维护最长公共子串寻找出路

注意这里是统计走过了多少点更新答案,不能直接通过\(len\)更新答案

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=600000;
LL nod,last,n,T;
LL len[maxn],fail[maxn],son[maxn][26];
char s[maxn];
inline void Insert(LL c){
LL np=++nod,p=last;
len[np]=len[p]+1;
last=np;
while(p&&!son[p][c]){
son[p][c]=np,
p=fail[p];
}
if(!p)
fail[np]=1;
else{
LL q=son[p][c];
if(len[q]==len[p]+1)
fail[np]=q;
else{
LL nq=++nod;
len[nq]=len[p]+1;
fail[nq]=fail[q];
memcpy(son[nq],son[q],sizeof(son[q]));
fail[np]=fail[q]=nq;
while(p&&son[p][c]==q){
son[p][c]=nq,
p=fail[p];
}
}
}
}
int main(){
nod=last=1;
scanf(" %s",s);
LL Len=strlen(s);
for(LL i=0;i<Len;++i)
Insert(s[i]-'a');
scanf(" %s",s);
LL ans=0,now=1,cnt=0;
Len=strlen(s);
for(LL i=0;i<Len;++i){
LL c=s[i]-'a';
if(son[now][c])
++cnt,
now=son[now][c];
else{
while(now&&!son[now][c])
now=fail[now];
if(!now)
cnt=0,
now=1;
else
cnt=len[now]+1,
now=son[now][c];
}
ans=max(ans,cnt);
}
printf("%lld\n",ans);
return 0;
}/*
fjewiofejhiofjmwopejeugfzjkjnfoakweldnfmoierhguiewkjfkowejrfoiwejsfd
jwierhdwuiek,dedjfkz[pjeowrfhuqigrfwerljfiuekdfkcdfheosf
*/

【SPOJ】Longest Common Substring的更多相关文章

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

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

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

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

  3. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  4. 【SPOJ1811】Longest Common Substring(后缀自动机)

    题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度 n<=250000 思路: #include<bits/stdc++.h> using namespace std; t ...

  5. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  6. SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)

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

  7. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  8. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  9. SPOJ 1812 Longest Common Substring II

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

随机推荐

  1. GRIT VIEW删除事件

    1.点选表格后找到事件 RowCommand 2.輸入gvGroupUser_RowCommand后双击                                        ------注分 ...

  2. alt和title的区别

    alt是html标签的属性,而title既是html标签,又是html属性. 在图像标签img中,除了常用的宽度width和高度height属性之外,还有两个比较重要并且也会用到的属性,就是alt和t ...

  3. java - day017 - 线程

    进程 操作系统中, 并行执行的任务 线程 进程内部, 并行执行的任务 进程包含线程 线程的创建 继承Thred 定义 Thred 的子类 重写 run() 方法 在 run() 方法中的代码, 是与其 ...

  4. Nginx.conf配置文件默认配置块略解

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  5. 用js刷剑指offer(数组中出现次数超过一半的数字)

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  6. github看项目-浏览器插件

    浏览器插件,使用浏览器看github项目 原文:浏览 GitHub 太卡了?教你两招!

  7. CMake---基础练习1

    因为卡在一个问题上,几经排除应该可能是CMakeLists.txt写的不正确,但是又生成了可执行文件,运行可执行文件报错.多方排除,应该是CMakeLists.txt加载动态库的时候,函数加载的不全. ...

  8. RTC — 软件协作开发管理平台

    IBM Rational Team Concert (简称RTC )是构建在IBM Rational面向软件交付技术的下一代协作平台Jazz平台上的一个商用产品.一个协作式的软件开发环境,它包含了集成 ...

  9. FRDM-KL43开发板驱动段式液晶SLCD的实现方法

    LCD的驱动不像LED那样,加上电压(LED实际上是电流驱动)就可以长期显示的. LCD驱动必须使用交流电压驱动才能保持稳定的显示,如果在LCD上加上稳定的直流电压, 不但不能正常显示,时间久了还会损 ...

  10. 链表(python)

    链表1.为什么需要链表顺序表的构建需要预先知道数据大小来申请连续的存储空间,而在进行扩充时又需要进行数据的搬迁,所以使用起来并不是很灵活.链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理. ...