SPOJ-LCS Longest Common Substring
题目连接:
https://vjudge.net/problem/SPOJ-LCS2
Description
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.
Sample Input
alsdfkjfjkdsal
fdjskalajfkdsla
Sample Output
3
Hint
题意
求两个串的最长公共子串长度
题解:
对a串建后缀自动机,b串在建好的自动机上跑,跑的过程和建树是一样的,如果Next[p][c]存在则nowlen++,否则p回退到fa[p]直到Next[p][c]存在,并更新nowlen为len[p]+1,对整个过程的nowlen取max就是答案
代码
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;
const int mx = 1e6+5;struct SAM_automaton {int Next[mx][26], len[mx], fa[mx];int last, tot;int newnode() {tot++;for (int i = 0; i < 26; i++) Next[tot][i] = 0;return tot;}void init() {tot = 0;last = newnode();}void add(int c) {int p = last;int np = last = newnode();len[np] = len[p] + 1;while (p && !Next[p][c]) {Next[p][c] = np;p = fa[p];}if (!p) fa[np] = 1;else {int q = Next[p][c];if (len[q] == len[p] + 1) fa[np] = q;else {int nq = newnode();len[nq] = len[p] + 1;fa[nq] = fa[q];for (int i = 0; i < 26; i++) Next[nq][i] = Next[q][i];fa[q] = fa[np] = nq;while (p && Next[p][c] == q) {Next[p][c] = nq;p = fa[p];}}}}
}SAM;char a[mx], b[mx], str[mx];int main() {SAM.init();scanf("%s%s", a, b);int lena = strlen(a);int lenb = strlen(b);for (int i = 0; i < lena; i++) SAM.add(a[i]-'a');int ans = 0, len = 0;for (int i = 0, p = 1; i < lenb; i++) {int c = b[i] - 'a';if (SAM.Next[p][c]) {len++;p = SAM.Next[p][c];} else {while (p && !SAM.Next[p][c]) p = SAM.fa[p];if (!p) {p = 1;len =0;} else {len = SAM.len[p] + 1;p = SAM.Next[p][c];}}ans = max(ans, len);}printf("%d\n", ans); return 0;
}
转载于:https://www.cnblogs.com/bpdwn-cnblogs/p/11213508.html
SPOJ-LCS Longest Common Substring相关推荐
- SPOJ LCS Longest Common Substring
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ - LCS Longest Common Substring(后缀自动机)
题目链接:点击查看 题目大意:给出两个字符串,求出其最长公共子串的长度 题目分析:可以对第一个字符串建立SAM,然后令第二个字符串在建好的SAM上跑就好了,如果遇到不能跑的点,就往上折返直到找到可以继 ...
- Spoj LCS2 - Longest Common Substring II
题目描述 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is ...
- SPOJ - LCS2 Longest Common Substring II(后缀自动机)
题目链接:点击查看 题目大意:给出n个字符串,求出最长公共子串的长度 题目分析:之前做过了求两个字符串最长公共子串的长度,相对来说那个题目还算是比较简单入门的,这个题目就稍微有点加大难度了,其实难度也 ...
- POJ 2774 Long Long Message SP1811 LCS - Longest Common Substring 题解
POJ:题目传送门 洛谷:题目传送门 题目大意: 求两个字符串的最长公共子串长度. 题解 后缀数组入门题,将两个字符串接在一起,中间用一个字符集以外的字符隔开,然后求出 h e i g h t hei ...
- spoj Longest Common Substring II
Longest Common Substring II SPOJ - LCS2 求10个串的LCS /*1.用第一个串建立后缀自动机2.len[s] 表示状态s 所能代表的字符串的最大长度mx[s] ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)【两种做法】
SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)[两种做法] 手动博客搬家: 本文发表于20181217 23:54:35, 原地址https: ...
- JavaScript实现longest Common Substring最长公共子串算法(附完整源码)
JavaScript实现longest Common Substring最长公共子串算法(附完整源码) longestCommonSubstring.js完整源代码 longestCommonSubs ...
- 最长公共子串LCS (Longest Common Subsequence) 算法
三个方法都有所借鉴,但代码部分是自己试着写出来的,虽然最后的运行结果都是正确的,但此过程中难免会有考虑不周全的地方,如发现代码某些地方有误,欢迎指正.同时有新的想法,也可以提出! 采用顺序结构存储串, ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
最新文章
- python制作生日祝福_【中信项目案例分享】python 实现自动生成个性化生日贺卡...
- Linux Makefile 中的陷阱【转】
- 云计算产值将超3000亿美元 亚马逊微软谷歌居三甲
- 企业微信:腾讯的“佛系”办公江湖
- C++ opengl 启动光照
- Create React App 2.0 华丽登场
- 25muduo_net库源码分析(一)
- 拆分是解决大规模应用问题的本质
- TabLayout+ViewPager+Fragment(内部:TabLayout+ViewPager+ Fragment)需要注意!!
- response.sendRedirect 报 java.lang.IllegalStateException 异常的解决思路
- 电商后台管理系统项目总结(一)
- MTK功能机RF射频参数计算与配置
- 游戏测试(面试可用)
- 刷榜中ASO优化中下载量与评论之间的对应比
- 应对微软20号黑屏的几种解决办法
- ESXI 6.7全面系统教程~esxi安装win10
- python 企查查爬虫_python爬虫另辟蹊径绕过企查查的登录验证,我太冇财了
- 个人计算机有ip地址吗,如何查看ip? 查看个人电脑IP地址五大方法
- Ubuntu安装显卡驱动及设置3d桌面
- python web server 知乎_“知乎网”技术方案初探