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

Notice:

new testcases added

思路


代码

#include <cstdio>
#include <cstring>const int maxn=250000;struct node
{node* tr[26];node* par;int maxl;node(int l=0){memset(tr,0,sizeof tr);par=NULL;maxl=l;}
};struct suffix_automaton
{node* qs;node* qlast;node* qn[maxn+10];int l[maxn+10];inline int clear(){delete qs;delete qlast;qlast=qs=new node();return 0;}inline int addchr(int ch){node* p=qlast;node* np=new node(qlast->maxl+1);qlast=np;while((p!=NULL)&&(p->tr[ch]==NULL)){p->tr[ch]=np;p=p->par;}if(p==NULL){np->par=qs;return 0;}node* q=p->tr[ch];if(q->maxl!=p->maxl+1){node* nq=new node(p->maxl+1);memcpy(nq->tr,q->tr,sizeof q->tr);nq->par=q->par;q->par=np->par=nq;while((p!=NULL)&&(p->tr[ch]==q)){p->tr[ch]=nq;p=p->par;}}else{np->par=q;}return 0;}inline int run(char* s,int len){qn[0]=qs;l[0]=0;for(register int i=1; i<=len; ++i){node* nq=qn[i-1];if(nq->tr[s[i]-'a']!=NULL){l[i]=l[i-1]+1;qn[i]=nq->tr[s[i]-'a'];continue;}while((nq!=NULL)&&(nq->tr[s[i]-'a']==NULL)){nq=nq->par;}if(nq==NULL){qn[i]=qs;l[i]=0;continue;}qn[i]=nq->tr[s[i]-'a'];l[i]=nq->maxl+1;}int ans=0;for(register int i=1; i<=len; ++i){if(l[i]>ans){ans=l[i];}}return ans;}
};suffix_automaton sam;
int la,lb,l[maxn+10];
char a[maxn+10],b[maxn+10];int main()
{scanf("%s%s",a+1,b+1);la=strlen(a+1);lb=strlen(b+1);sam.clear();for(register int i=1; i<=lb; ++i){sam.addchr(b[i]-'a');}printf("%d\n",sam.run(a,la));return 0;
}

转载于:https://www.cnblogs.com/Canopus-wym/p/10376227.html

SPOJ LCS Longest Common Substring相关推荐

  1. SPOJ - LCS Longest Common Substring(后缀自动机)

    题目链接:点击查看 题目大意:给出两个字符串,求出其最长公共子串的长度 题目分析:可以对第一个字符串建立SAM,然后令第二个字符串在建好的SAM上跑就好了,如果遇到不能跑的点,就往上折返直到找到可以继 ...

  2. Spoj LCS2 - Longest Common Substring II

    题目描述 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is ...

  3. SPOJ - LCS2 Longest Common Substring II(后缀自动机)

    题目链接:点击查看 题目大意:给出n个字符串,求出最长公共子串的长度 题目分析:之前做过了求两个字符串最长公共子串的长度,相对来说那个题目还算是比较简单入门的,这个题目就稍微有点加大难度了,其实难度也 ...

  4. POJ 2774 Long Long Message SP1811 LCS - Longest Common Substring 题解

    POJ:题目传送门 洛谷:题目传送门 题目大意: 求两个字符串的最长公共子串长度. 题解 后缀数组入门题,将两个字符串接在一起,中间用一个字符集以外的字符隔开,然后求出 h e i g h t hei ...

  5. spoj Longest Common Substring II

    Longest Common Substring II SPOJ - LCS2 求10个串的LCS /*1.用第一个串建立后缀自动机2.len[s] 表示状态s 所能代表的字符串的最大长度mx[s] ...

  6. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)【两种做法】

    SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)[两种做法] 手动博客搬家: 本文发表于20181217 23:54:35, 原地址https: ...

  7. JavaScript实现longest Common Substring最长公共子串算法(附完整源码)

    JavaScript实现longest Common Substring最长公共子串算法(附完整源码) longestCommonSubstring.js完整源代码 longestCommonSubs ...

  8. 最长公共子串LCS (Longest Common Subsequence) 算法

    三个方法都有所借鉴,但代码部分是自己试着写出来的,虽然最后的运行结果都是正确的,但此过程中难免会有考虑不周全的地方,如发现代码某些地方有误,欢迎指正.同时有新的想法,也可以提出! 采用顺序结构存储串, ...

  9. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

最新文章

  1. 有 Bug 不会调试 ? 这篇文章很详细 !
  2. C++11中Thread类简单使用的例子
  3. opencv学习笔记2:图像处理基础
  4. SSL 的 java 实现
  5. mysql实战38 | 都说InnoDB好,那还要不要使用Memory引擎?
  6. java面试题11 牛客:如下语句通过算术运算和逻辑运算之后i和 j的结果是
  7. 到底什么是空指针?如何避免空指针_01
  8. 草稿-乱-爬虫-post请求数据与Request Payload
  9. 后台模板 开源_3个开源样板网页设计模板
  10. matlab错误打印,MATLAB中数据输出格式出现错误?
  11. Iometer存储测试工具参数说明-图形主界面(整理)
  12. Spring Security OAuth2.0_实现分布式认证授权_集成测试_Spring Security OAuth2.0认证授权---springcloud工作笔记155
  13. 二分法05:搜索旋转排序数组
  14. flag - 待浏览学习网站
  15. 汇编版|电子印章在各类业务文件中的应用
  16. 4.3.2 信道编码 ——卷积码
  17. com.mysql.jdbc.Connection.isValid(I)Z错误解决办法
  18. Oracle ora-12514报错解决方法
  19. 社群裂变工具有哪些?裂变活动成功的4个关键点!
  20. try/catch 的解释与用法

热门文章

  1. 经典题---数组改“貌”
  2. Shell脚本判断IP是否合法性(多种方法)
  3. 架构师书单 2nd Edition--转载
  4. Spring_hibernate整合初步 based in annotation
  5. ADO.NET连接SQL Server数据库
  6. 唐山一个葬礼上的豪华车队
  7. Zenoss Announces Monitoring for VMWare's Cloud Director
  8. 典型问题分析(十五)
  9. TreeMap源码分析,看了都说好
  10. 使用负载均衡SLB IPv6搞定苹果AppStore审核