Description

给出两个字符串,求最长公共子串.

Sol

SAM.

这题随便做啊...后缀数组/Hash+二分都可以.

SAM就是模板啊...直接在SAM上跑就行,没有了 \(go[w]\) 就往 \(par\) 跳.

每走一步统计一下答案.

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int N = 250005; struct State{
State *par,*go[26];
int val;
State(int _val):par(0),val(_val){ memset(go,0,sizeof(go)); }
}*rt,*lst;
void extend(int w){
State *p=lst,*np=new State(p->val+1);
while(p && p->go[w]==0) p->go[w]=np,p=p->par;
if(!p) np->par=rt;
else{
State *q=p->go[w];
if(q->val == p->val+1) np->par=q;
else{
State *nq=new State(p->val+1);
memcpy(nq->go,q->go,sizeof(q->go));
nq->par=q->par;
np->par=q->par=nq;
while(p && p->go[w] == q) p->go[w]=nq,p=p->par;
}
}lst=np;
} char A[N],B[N];
int la,lb,ans,len; void work(){
State *t=rt;
for(int i=0;i<lb;i++){
for(;t && !t->go[B[i]-'a'];len=(t=t->par) ? t->val : 0);
if(t && t->go[B[i]-'a']) t=t->go[B[i]-'a'],len++;else t=rt;
ans=max(ans,len);
}
}
int main(){
rt=new State(0),lst=rt; scanf("%s%s",A,B);
la=strlen(A),lb=strlen(B); for(int i=0;i<la;i++) extend(A[i]-'a'); work();
return cout<<ans<<endl,0;
}

  

SPOJ 1811 Longest Common Substring的更多相关文章

  1. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

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

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

  3. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

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

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

  5. ●SPOJ 1811 Longest Common Substring

    题链: http://poj.org/problem?id=2774 题解: 求两个字符串(S,T)的最长公共子串.对 S串建后缀自动机.接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最 ...

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

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

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

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

  8. 【SPOJ】Longest Common Substring II

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

  9. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

随机推荐

  1. JavaWeb学习笔记——javabean

  2. MySQL学习笔记——函数

    常用函数 ALTER TABLE tb_emp ); #插入数据 INSERT INTO tb_dept() VALUE(,'市场部','负责市场工作'); # concat 连接 SELECT CO ...

  3. C#----使用WindowsMediaPlayer 同时播放多个声音

    使用Windows Media Player 其实就是使用组件AxWindowsMediaPlayer. 添加两个引用:Interop.WMPLib.dll和AxInterop.WMPLib.dll. ...

  4. #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0

    头文件处理 #import <UIKit/UIKit.h> #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 #else #imp ...

  5. yum clean all 是什么意思

    yum会将下载下来的 包文件rpm和头文件header存盘在 本地机器的硬盘 缓存中, 这个将占用 硬盘空间, 可以将这些内容清除掉, 以释放磁盘空间: yum clean headers: // 释 ...

  6. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

  7. 使用DOS比较两个txt文件的差异

    将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异

  8. 2015年11月26日 Java基础系列(四)class的定义,继承和实现interface

    序,类的设计是JAVA操作的核心,面对对象思想中一切皆对象. 一.类定义中的变量 静态成员变量,为类所有,称为类变量:只有一份,编译时即分配值,使用关键字static声明. 非静态成员变量,每个实例一 ...

  9. (2)apply函数及其源码

      本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...

  10. SQL Server 服务器器信息备份(一)--login新建脚本备份

    前言 若你的企业使用SQL Server数据库镜像为容灾技术. 那你一定做过在镜像切换之前要新建Login,而且若Login密码不同,要修改链接数据库的字符串,在切换完之后则仍需要给数据库重新赋予权限 ...