题目链接

对第一个串建出\(SAM\),然后用第二个串去匹配。

如果能往下走就往下走,不能的话就跳parent tree的父亲,直到能走为止。如果跳到\(0\)了还是不能走,重新匹配。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. const int MAXN = 1000010;
  6. struct SAM{
  7. int ch[26];
  8. int len, fa;
  9. }sam[MAXN << 1];
  10. int las = 1, cnt = 1;
  11. inline void add(int c){
  12. int p = las; int np = las = ++cnt;
  13. sam[np].len = sam[p].len + 1;
  14. for(; p && !sam[p].ch[c]; p = sam[p].fa) sam[p].ch[c] = np;
  15. if(!p) sam[np].fa = 1;
  16. else{
  17. int q = sam[p].ch[c];
  18. if(sam[q].len == sam[p].len + 1) sam[np].fa = q;
  19. else{
  20. int nq = ++cnt; sam[nq] = sam[q];
  21. sam[nq].len = sam[p].len + 1;
  22. sam[q].fa = sam[np].fa = nq;
  23. for(; p && sam[p].ch[c] == q; p = sam[p].fa) sam[p].ch[c] = nq;
  24. }
  25. }
  26. }
  27. char a[MAXN], b[MAXN];
  28. int ans;
  29. int main(){
  30. scanf("%s", a + 1);
  31. scanf("%s", b + 1);
  32. int lena = strlen(a + 1), lenb = strlen(b + 1);
  33. for(int i = 1; i <= lena; ++i)
  34. add(a[i] - 'a');
  35. int p = 1, len = 0;
  36. for(int i = 1; i <= lenb; ++i){
  37. if(sam[p].ch[b[i] - 'a'])
  38. ++len, p = sam[p].ch[b[i] - 'a'];
  39. else{
  40. while(!sam[p].ch[b[i] - 'a'] && p) p = sam[p].fa;
  41. if(!p) p = 1, len = 0;
  42. else len = sam[p].len + 1, p = sam[p].ch[b[i] - 'a'];
  43. }
  44. ans = max(ans, len);
  45. }
  46. printf("%d\n", ans);
  47. return 0;
  48. }

【SP1811】 LCS - Longest Common Substring(后缀自动机)的更多相关文章

  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 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  3. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  4. SPOJ 1811 Longest Common Substring 后缀自动机

    模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...

  5. SP1811 LCS - Longest Common Substring

    \(\color{#0066ff}{ 题目描述 }\) 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . \(\color{#0066ff}{输 ...

  6. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...

  7. 【题解】SP1811 LCS - Longest Common Substring

    \(\color{purple}{Link}\) \(\text{Solution:}\) 题目要求找到两个串的最长公共子串.\(LCP\) 我们将两个串中间和末尾插入终止符,并弄到一棵后缀树上去. ...

  8. 「双串最长公共子串」SP1811 LCS - Longest Common Substring

    知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...

  9. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  10. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

随机推荐

  1. fluent计算输出时均值

    / ****************************************************************************** * Created on : 2017 ...

  2. go 牛顿法开平方

    func main() { fmt.Println(sqrt(3)) } func sqrt(x float64)float64{ z := x for i := 0; i < 10 ; i++ ...

  3. [RoarCTF]Easy Calc

    目录 [RoarCTF]Easy Calc 知识点 1.http走私绕过WAF 2.php字符串解析特性绕过WAF 3.绕过过滤写shell [RoarCTF]Easy Calc 题目复现链接:htt ...

  4. Spark安装(standalone)

    文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...

  5. uniapp - 键盘弹起背景图片不会被挤压

    [释义] uni.getSystemInfoSync()获取屏幕可用高度windowScreen做为背景图高度即可(非虚拟DOM也可以使用本思路). [源码] <template> < ...

  6. TCP的拥塞窗口和快速恢复机制的一些备忘及一点想法

    rwnd(窗口,代表接收端的处理能力).cwnd(拥塞窗口,从发送端看当前网络整体承载能力).ssthresh(快速增长切换成慢速增长的界限值) 1.慢启动,是指数增长(对面确认多少个包,就增加多少) ...

  7. python去除BOM头\ufeff等特殊字符

    1.\ufeff 字节顺序标记 去掉\ufeff,只需改一下编码就行,把UTF-8编码改成UTF-8-sigwith open(file_path, mode='r', encoding='UTF-8 ...

  8. Java多个线程顺序打印数字

    要求 启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3 方法一: 使用synchronized 三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打 ...

  9. NFS的安装与使用

    一.服务器端: 1.1安装NFS服务: #执行以下命令安装NFS服务器, #apt会自动安装nfs-common.rpcbind等13个软件包 sudo apt install nfs-kernel- ...

  10. win 10 关闭或打开 测试模式

    一.关闭测试模式 方法: 以管理员身份运行 cmd 运行:bcdedit /set testsigning off 重启电脑 二.开启测试模式 以管理员身份运行 cmd 运行:bcdedit /set ...