\(\color{#0066ff}{ 题目描述 }\)

输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串。如果没有公共子串则输出0 。

\(\color{#0066ff}{输入格式}\)

两个字符串

\(\color{#0066ff}{输出格式}\)

一个整数,为 所求答案

\(\color{#0066ff}{输入样例}\)

  1. alsdfkjfjkdsal
  2. fdjskalajfkdsla

\(\color{#0066ff}{输出样例}\)

  1. 3

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{ 题解 }\)

对第一个串建立后缀自动机

用第二个串在自动机上跑匹配

匹配一下就取max

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define LL long long
  4. LL in() {
  5. char ch; int x = 0, f = 1;
  6. while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
  7. for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
  8. return x * f;
  9. }
  10. const int maxn = 6e5 + 5;
  11. struct SAM {
  12. protected:
  13. struct node {
  14. node *ch[26], *fa;
  15. int len, siz;
  16. node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
  17. memset(ch, 0, sizeof ch);
  18. }
  19. };
  20. node *root, *tail, *lst;
  21. node pool[maxn];
  22. void extend(int c) {
  23. node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
  24. for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
  25. if(!v) o->fa = root;
  26. else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
  27. else {
  28. node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
  29. std::copy(d->ch, d->ch + 26, n->ch);
  30. n->fa = d->fa, d->fa = o->fa = n;
  31. for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
  32. }
  33. lst = o;
  34. }
  35. void clr() {
  36. tail = pool;
  37. root = lst = new(tail++) node();
  38. }
  39. public:
  40. SAM() { clr(); }
  41. void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
  42. int match(char *s) {
  43. int ans = 0;
  44. node *o = root;
  45. int len = 0;
  46. for(char *p = s; *p; p++) {
  47. int pos = *p - 'a';
  48. if(o->ch[pos]) o = o->ch[pos], len++;
  49. else {
  50. while(o && !o->ch[pos]) o = o->fa;
  51. if(!o) o = root, len = 0;
  52. else len = o->len + 1, o = o->ch[pos];
  53. }
  54. ans = std::max(ans, len);
  55. }
  56. return ans;
  57. }
  58. }sam;
  59. char s[maxn];
  60. int main() {
  61. scanf("%s", s);
  62. sam.ins(s);
  63. scanf("%s", s);
  64. printf("%d\n", sam.match(s));
  65. return 0;
  66. }

SP1811 LCS - Longest Common Substring的更多相关文章

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

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

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

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

  3. 【SP1811】LCS - Longest Common Substring

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

  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. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  6. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

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

  7. SPOJ 1811 LCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...

  8. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  9. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

随机推荐

  1. git 学习 多个提交用同一个commit

    git add .git commit --amend(连续按连个ZZ)git push -f origin ibm_branch(命令行可能不好用,用IDEA force push好用)

  2. 【转】Rails 4中使用 Bootstrap 3

    转自:http://rvg.me/2013/11/using-bootstrap-3-with-rails-4/ If you are looking to use Bootstrap 3 with ...

  3. springmvc----demo2---a->b--bai

    1.jsp 2.jsp 3.jsp LianxiAction: package com.etc.controller; import javax.servlet.http.HttpSession; i ...

  4. Python的IDE:Eclipse+PyDev配置

    最近准备学习python的开发了,当然主要先尝试web方面的开发,个人所学的主要就是javaweb方面,出去了一趟,感觉到了自己的狠多不足,当然也想对自己重新定位一下,不想以后出去只是码畜级别的.想学 ...

  5. 向vsftp服务器上传文件报“550 Permission denied”错误的解决办法

    上传文件: ftp> mput db.iso 550 Permission denied 原因:vsftp默认配置不允许上传文件. 解决:修改/etc/vsftpd.conf 将“write_e ...

  6. Markdown编辑器及图床推荐

    Typora和自动图床工具 Typora 地址 ,极致简洁,界面很漂亮,最重要的是所见即所得 百度云搬运 密码:xi01 自动图床工具 需要七牛云做图床,感谢作者,详见博客 使用方法,只需两步即可完成 ...

  7. android文件缓存管理

    缓存类  : public class ConfigCache { private static final String TAG = ConfigCache.class.getName(); pub ...

  8. 第3章 springboot接口返回json 3-2 Jackson的基本演绎法

    @JsonIgnore private String password; @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a",locale=&q ...

  9. centos系统查看本机IP地址

    centos系统查看本机IP地址,输入 ifconfig -a查看 centos查询上网公网IP输入 curl ifconfig.me 命令即可查看 centos查询上网网关IP,tracepath  ...

  10. WINFORM 无边框窗体 阴影与移动

    //窗体移动API[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport(&q ...