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. Oracle学习之常见问题处理

    转自:http://blog.csdn.net/liusong0605/article/details/16349121 安装完oracle并启动服务后,通过sqlPlus无法登录,出现如下错误: s ...

  2. Java的函数与函数重载

    关于Java的函数与函数重载 关于Java的函数与函数重载 1. 函数 假设有一个游戏程序,程序在运行过程中,要不断地发射炮弹.发射炮弹的动作都需要使用一段百行左右的程序代码,在每次发射炮弹的地方都要 ...

  3. codeforces 632C. The Smallest String Concatenation 排序

    题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...

  4. Gulp browserify livereload

    Gulp browserify livereload 之前在browserify那个博文中介绍了gulp + browserify 不过那个配置还不能满足日常需要 搬运 https://github. ...

  5. [问题解决] File "/struts-tags" not found

    错误:org.apache.jasper.JasperException: File "/struts-tags" not found 发生场景:tomcat服务器 解决方案:将t ...

  6. 10个必备的移动UI设计资源站

    http://www.uisdc.com/10-necessary-mobile-ui-design-resources# 交互设计中如何增加趣味性.提升愉悦http://www.uisdc.com/ ...

  7. 《Java4Android视频教程》学习笔记(二)

    一:面向对象 1.对象 ①对象的使用方法 对象.变量 对象.方法 ②匿名对象 new A().方法 new A().变量 匿名对象会被分配到对内存中 java内存处理机制会对一定时间内无指针指向的对象 ...

  8. string s = HttpContext.Current.Server.MapPath("");

    string s = HttpContext.Current.Server.MapPath(""); 获取当前文件夹路径 而后用相对路径读取图片

  9. SQL Server 动态行转列(轉載)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段; 方法二:使用拼接SQL, ...

  10. solr6环境搭建

    1.下载并安装jdk1.8,配置环境变量 2.下载并安装tomcat8(8以上) 3.下载solr源码,转变部署solr a)[solr-6.2.0\server\solr-webapp]下的weba ...