SPOJ 1811 Longest Common Substring
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 fdjskalajfkdslaOutput: 3 题解:哈哈很意外啊,在电脑城随便找台机子用洛谷在线IDE 1A这题啊讲讲思路:以A建立SAM 让B在SAM上匹配可以类比于kmp思想,我们知道在Parent树上,fa是当前节点的子集,也就是说满足最大前缀,利用这个就可以做题了步骤如下:1.如果满足ch[p][c]存在,那么就直接走就是了,和AC自动机类似,len++2.如果不存在,那么就沿fa上跳,找到最大dis[fa]且满足存在c这个儿子的.类似于跳fail链3.跳到根就直接初始化,重新开始
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 const int N=550005; 8 char S[N];int n=0,cnt=1,cur=1,last=1,ch[N][26],fa[N],dis[N]; 9 void build(int c){ 10 last=cur;cur=++cnt;dis[cur]=++n; 11 int p=last; 12 for(;p && !ch[p][c];p=fa[p])ch[p][c]=cur; 13 if(!p)fa[cur]=1; 14 else{ 15 int q=ch[p][c]; 16 if(dis[q]==dis[p]+1)fa[cur]=q; 17 else{ 18 int nt=++cnt;dis[nt]=dis[p]+1; 19 memcpy(ch[nt],ch[q],sizeof(ch[q])); 20 fa[nt]=fa[q];fa[q]=fa[cur]=nt; 21 for(;ch[p][c]==q;p=fa[p])ch[p][c]=nt; 22 } 23 } 24 } 25 int ans=0; 26 void Flr() 27 { 28 int p=1,nxt,lt; 29 scanf("%s",S); 30 int l=strlen(S),len=0,c; 31 for(int i=0;i<l;i++){ 32 c=S[i]-'a'; 33 if(ch[p][c]){ 34 len++;p=ch[p][c]; 35 } 36 else{ 37 lt=0;nxt=0; 38 while(p!=1){ 39 if(ch[p][c] && dis[p]>lt)lt=dis[p],nxt=p; 40 p=fa[p]; 41 } 42 if(nxt) 43 p=ch[nxt][c],len=dis[nxt]+1; 44 else 45 p=1,len=0; 46 } 47 if(len>ans)ans=len; 48 } 49 printf("%d\n",ans); 50 } 51 void work() 52 { 53 scanf("%s",S); 54 for(int i=0,l=strlen(S);i<l;i++)build(S[i]-'a'); 55 Flr(); 56 } 57 int main() 58 { 59 work(); 60 return 0; 61 }
转载于:https://www.cnblogs.com/Yuzao/p/7273413.html
SPOJ 1811 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 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个字符串,求出最长公共子串的长度 题目分析:之前做过了求两个字符串最长公共子串的长度,相对来说那个题目还算是比较简单入门的,这个题目就稍微有点加大难度了,其实难度也 ...
- SPOJ - LCS Longest Common Substring(后缀自动机)
题目链接:点击查看 题目大意:给出两个字符串,求出其最长公共子串的长度 题目分析:可以对第一个字符串建立SAM,然后令第二个字符串在建好的SAM上跑就好了,如果遇到不能跑的点,就往上折返直到找到可以继 ...
- 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 ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 省选专练之后缀自动机SPOJ1811LCS - Longest Common Substring
陈老师引入SAM的例题 求两个串的LCS 直接建一个串的SAM,并且把另外一个串放进去跳. 这个方法很经典! 几乎满足了所有两个串的公共串问题的所有解法 有巨子说这个跳是均摊Log的,我也不知道QwQ ...
- Longest Common Substring
http://acm.hdu.edu.cn/showproblem.php?pid=1403 题解:后缀数组+最长公共子串 /* *@Author: STZG *@Language: C++ */ # ...
最新文章
- 2021年大数据Spark(二十二):内核原理
- PV操作——生产者和消费者
- 每日一皮:自己运行正常,测试一测就有bug
- 远程控制工具_不要让您的工具控制您
- axios 配置loading_用Axios Element 实现全局的请求 loading
- linux ps进程管理命令,Linux 进程管理命令之ps
- python3 爬虫实战:为爬虫添加 GUI 图像界面 之 tkinter
- 最短路径 | 1087 三重标尺+记录最短路径条数
- 米斯特白帽培训讲义 漏洞篇 XSS
- Spark的简介和在大数据领域的地位
- 计算机黑屏的原因及修复,导致笔记本电脑开机黑屏的原因以及对应的修复方法...
- 《电路(邱关源)》第五版重难点记录(长期更新)
- 为什么说跳槽加薪低于30%,等于在“降薪”?
- Wex5 app 学习笔记(二)
- Json Schema 是什么?
- Java继承同时实现接口 以及继承方法的使用
- Manacher算法的基础应用:小A的回文串
- python爬虫-采集英语翻译
- 面试题...觉得挺经典的
- construct2制作炮击小箱子小游戏
热门文章
- new与delete函数使用方法以及注意事项
- mysql字符类型_MySQL学习分享--字符类型
- android桌面adw,ADW Launcher
- 计算机教育考试多少分及格,全国职称计算机考试多少分及格?
- 数据清洗+特征构造:application_{train|test}.csv
- 实战 Kaggle 比赛:狗的品种识别(ImageNet Dogs) 动手学深度学习v2 pytorch
- 阿里云云计算 52在线实验--云监控初体验
- 极客大学架构师训练营--食堂就餐系统架构设计⽂档 -- 第一次作业
- Input and Output-HackerRank C++
- 593. 有效的正方形