1811. Longest Common Substring

Problem code: LCS

A string is finite sequence of characters over a non-empty finite set Σ.

In this problem, Σ is the set of lowercase letters.

Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.

Now your task is simple, for two given strings, find the length of the longest common substring of them.

Here common substring means a substring of two or more strings.

Input

The input contains exactly two lines, each line consists of no more than 250000 lowercase letters, representing a string.

Output

The length of the longest common substring. If such string doesn't exist, print "0" instead.

Example

Input:
alsdfkjfjkdsal
fdjskalajfkdsla Output:
3

本题要求2个字符串的LCS.

那么先将A串读入,再在A串上匹配B串显然

但是一个结点能表示的后缀有|maxS|-|minS|+1个。

那么就这样:

假设当前在结点x,下一个B串要匹配的字母为c

如果x有ch[c],走过去len+1

否则找pre,更新len=pre的maxS

理由如下:

设原有串=ababd,babd,abd ,匹配为其中某一个

不存在abdx无法匹配.

取pre=bd,d,(由定义可知不存在‘dc存在匹配,bdc不存在’的情况,故它俩能为1个状态)

那么len=pre的maxS(step)

否则返回根,len=0

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (500000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
char s1[MAXN],s2[MAXN];
struct node
{
int pre,step,ch[26];
char c;
node(){pre=step=c=0;memset(ch,sizeof(ch),0);}
}a[MAXN];
int last=0,total=0;
void insert(char c)
{
int np=++total;a[np].c=c+'a',a[np].step=a[last].step+1;
int p=last;
for(;!a[p].ch[c];p=a[p].pre) a[p].ch[c]=np;
if (a[p].ch[c]==np) a[np].pre=p;
else
{
int q=a[p].ch[c];
if (a[q].step>a[p].step+1)
{
int nq=++total;a[nq]=a[q];a[nq].step=a[p].step+1;
a[np].pre=a[q].pre=nq;
for(;a[p].ch[c]==q;p=a[p].pre) a[p].ch[c]=nq;
}else a[np].pre=q;
}
last=np;
}
int main()
{
// freopen("spojLCS.in","r",stdin);
scanf("%s%s",s1+1,s2+1);int n=strlen(s1+1),m=strlen(s2+1);
For(i,n) insert(s1[i]-'a');
int now=0,ans=0,len=0;
For(i,m)
{
char c=s2[i]-'a';
while (now&&!a[now].ch[c]) now=a[now].pre,len=a[now].step;
if (a[now].ch[c]) now=a[now].ch[c],len++;
ans=max(ans,len);
}
printf("%d\n",ans); return 0;
}

SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)的更多相关文章

  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. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

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

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

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

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

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

  7. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

  8. SPOJ LCS - Longest Common Substring 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...

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

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

随机推荐

  1. 正整数从1到N,输出按照字典序排序的前K个数

    #include <iostream> #include <cassert> using namespace std; ; char a[max_len]; void topK ...

  2. ring3 dll hide

    ZwQuerySystemInformation(SystemProcessInformation,SystemInformation,Length,ReturnLength);         pS ...

  3. windows8 安装IIS 和 添加网站(转)

    Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...

  4. Java中的流程控制(三)

    关于Java中的流程控制 关于Java中的流程控制 4.do while语句 do while语句的功能和while语句差不多,只不过它是在执行完第一次循环后才检测条件表达式的值,这意味着包含在大括号 ...

  5. ActiveMQ的入门demo

    步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...

  6. ngrok原理浅析(转载)

    之前在进行 微信Demo开发时曾用到过 ngrok这个强大的tunnel(隧道)工具,ngrok在其github官方页面上的自我诠释是 "introspected tunnels to lo ...

  7. Making your first driver - complete walkthrough(使用VisualDDK)

    This article describes how to create, build and debug your first driver using Visual Studio and Visu ...

  8. QT5.4 计算器程序 打包&发布,解决dll的最新解决方案(图文并茂,很清楚)

    QT写界面还是很不错,就是打包会比较麻烦,折腾了一天总算是打包完成了. QT软件的打包发布一个难点是必备dll文件的识别,现在高版本QT自带了一个windeployqt工具,直接会把需要的dll生成一 ...

  9. php 接口示例

    php 接口示例: public function dev(){ $m=new Model('machine_info'); $ip=$_GET['ip']; echo $ip; //$arr=$m- ...

  10. Installing perl and writing your first perl program in Ubuntu

    Installing perl and writing your first perl program in Ubuntu     Installing perl and writing your f ...