SP1811 【LCS - Longest Common Substring】
\(SAM\)上匹配
我们就是需要找到两个串的最长公共子串
先对其中一个串建出\(SAM\),之后我们把另一个串放到上面跑
如果当前在\(SAM\)的状态是\(now\),下一个字符是\(c\),匹配出的的长度为\(L\)
如果\(now\)有\(c\)这个转移,我们就转移过去,\(L\)++
如果没有我们就跳\(link\),知道跳到有这个转移为止,同时把\(L\)搞成新状态的\(len\)
这样做就好了
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long
#define maxn 500005
#define max(a,b) ((a)>(b)?(a):(b))
char S[maxn>>1],T[maxn>>1];
int lst=1,n,m,len[maxn],pre[maxn],son[maxn][26];
int now=1,L,ans,cnt=1;
inline void ins(int c)
{
int f=lst,p=++cnt; lst=p;
len[p]=len[f]+1;
while(f&&!son[f][c]) {son[f][c]=p;f=pre[f];}
if(!f) {pre[p]=1;return;}
int x=son[f][c];
if(len[f]+1==len[x]) {pre[p]=x;return;}
int y=++cnt;
len[y]=len[f]+1;pre[y]=pre[x];pre[x]=pre[p]=y;
for(int i=0;i<26;i++) son[y][i]=son[x][i];
while(f&&son[f][c]==x) {son[f][c]=y;f=pre[f];}
}
inline void q(int c)
{
if(son[now][c]) {now=son[now][c];L++;ans=max(ans,L);return;}
while(now&&!son[now][c]) now=pre[now];
if(!now) {now=1,L=0;return;}
L=len[now]+1;now=son[now][c];ans=max(ans,L);
}
int main()
{
scanf("%s",S+1);n=strlen(S+1);
for(int i=1;i<=n;i++) ins((int)(S[i]-'a'));
scanf("%s",T+1);n=strlen(T+1);
for(int i=1;i<=n;i++) q((int)(T[i]-'a'));
printf("%d\n",ans);
return 0;
}
SP1811 【LCS - Longest Common Substring】的更多相关文章
- 【题解】SP10570 【LONGCS - Longest Common Substring】
\(\color{Red}{Link}\) \(\text{Solution:}\) 还是\(\text{Suffix Tree.}\) 根据\(\color{Blue}{Link}\)我们可以得到一 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- 【刷题】SPOJ 1811 LCS - Longest Common Substring
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 LCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...
随机推荐
- iView 初识
iView和element-UI在table这块有有相似之处,但是与layui有不同的地方 在data数据这里有明显的不同,在iView中data数组下每个元素对象对应一行的数据:而layui中,da ...
- Rsa2加密报错java.security.spec.InvalidKeySpecException的解决办法
最近在和支付宝支付做个对接,Java项目中用到了RSA2进行加解密,在加密过程中遇到了错误: java.security.spec.InvalidKeySpecException: java.secu ...
- springboot项目中引用其他springboot项目jar
1. 剔除要引入的springboot项目中不需要的文件:如Application和ApplicationTests等 2.打包 不能使用springboot项目自带的打包插件进行打包: 3.打包 4 ...
- 学习javscript对象笔记(一)
对象(Objects) 对象属性值可以是除undefined值之外的任意值 1.对象字面量 a.{} b.new object(); 注意事项: 在对象字面量中,如果属性名是一个合法的javasc ...
- oracle中的exists理解
select * from EB where exists (select * from BB where Code=EB.Code) 把select 外层表EB看成是循环的,把每一个值eb.code ...
- 我们能用canvas做什么?
什么是Canvas? Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像.Canvas由一个可绘制地区HTML代码中的属性定义决定高度和宽度.JavaScript代码可以访问该地区,通过 ...
- axios请求报Uncaught (in promise) Error: Request failed with status code 404
使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...
- sql时间区间查询性能测试
这个测试针对"一个月"的区间来查询数据.分datetime类型和nvachar类型. 先比较datetime类型: 一.datediff函数 declare @beginTime ...
- IntelliJ IDEA 2017.2 下载和破解方法
一.IntelliJ IDEA 2017 下载地址 http://www.jetbrains.com/idea/#chooseYourEdition 要下载付费版的,免费版的很多功能不能用 二.破解 ...
- C/C++中的auto关键词
C语言 auto被解释为一个自动存储变量的关键字,也就是申明一块临时的变量内存. 例如: auto double a=3.7; 表示a为一个自动存储的临时变量. C++语言 C++ 98标准/C++0 ...