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. day5_python学习笔记_chapter6_字符串列表元组

    1. 序列:seq[n], seq[x:y], seq * n序列重复n次,切片, 序列翻转 s=”abcde", s[::-1]="edcba" 内建函数:1. 类型转 ...

  2. easyui 验证控件 tooltip message显示位置

    找了半天才发现是这个属性在控制,tipPosition:'left',官网那个demo,误人子弟.

  3. [Java]编写自己的Matrix矩阵类

    用java实现一个简单的矩阵类,可以实现简单的矩阵计算功能. class Matrix 1.向量点乘 public static double dot(double[] x,double[] y) 2 ...

  4. printf与scanf的返回值、注意点

    printf()的返回值为其输出字符串常量的字符数(注意字符数与字数的区别)注意计数针对所有的打印字符,包括空格和不可见的换行字符(不包括字符串的空字符). scanf()函数返回成功读入的项目的个数 ...

  5. IOS 特定于设备的开发:基于加速计的滚动视图

    倾斜滚轮使用设备的内置加速计来控制在UIScrollView的内容周围移动.当用户调增设备时,材料会相应的下落,他不会把视图定位在屏幕上,而是把内容视图滚动到一个新的偏移位置. 创建这个界面的挑战在于 ...

  6. lock table

    1.在执行lock table语句后,则在执行unlock tables之前,当前会话只能操作当前被锁定的表(包括表别名)2.read锁,其它会话只有读取权限,没有写入权限3.write锁,其它会话只 ...

  7. 监控Informix-Url

    jdbc:informix-sqli://[{ip-address|host-name}:{port-number|service-name}][/dbname]: INFORMIXSERVER=se ...

  8. Protel99se教程一:建立一个数据库文件

    学习Protel99 SE的第一步,是建立一个DDB文件,也就是说,使用protel99se进行电路图和PCB设计,以及其它的数据,都存放在一个统一的DDB数据库中的 一.打开protel 99se后 ...

  9. spring+springMVC集成(annotation方式)

    spring+springMVC集成(annotation方式) SpringMVC+Spring4.0+Hibernate 简单的整合 MyBatis3整合Spring3.SpringMVC3

  10. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...