\(SAM\)上匹配

我们就是需要找到两个串的最长公共子串

先对其中一个串建出\(SAM\),之后我们把另一个串放到上面跑

如果当前在\(SAM\)的状态是\(now\),下一个字符是\(c\),匹配出的的长度为\(L\)

  • 如果\(now\)有\(c\)这个转移,我们就转移过去,\(L\)++

  • 如果没有我们就跳\(link\),知道跳到有这个转移为止,同时把\(L\)搞成新状态的\(len\)

这样做就好了

代码

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #define LL long long
  6. #define maxn 500005
  7. #define max(a,b) ((a)>(b)?(a):(b))
  8. char S[maxn>>1],T[maxn>>1];
  9. int lst=1,n,m,len[maxn],pre[maxn],son[maxn][26];
  10. int now=1,L,ans,cnt=1;
  11. inline void ins(int c)
  12. {
  13. int f=lst,p=++cnt; lst=p;
  14. len[p]=len[f]+1;
  15. while(f&&!son[f][c]) {son[f][c]=p;f=pre[f];}
  16. if(!f) {pre[p]=1;return;}
  17. int x=son[f][c];
  18. if(len[f]+1==len[x]) {pre[p]=x;return;}
  19. int y=++cnt;
  20. len[y]=len[f]+1;pre[y]=pre[x];pre[x]=pre[p]=y;
  21. for(int i=0;i<26;i++) son[y][i]=son[x][i];
  22. while(f&&son[f][c]==x) {son[f][c]=y;f=pre[f];}
  23. }
  24. inline void q(int c)
  25. {
  26. if(son[now][c]) {now=son[now][c];L++;ans=max(ans,L);return;}
  27. while(now&&!son[now][c]) now=pre[now];
  28. if(!now) {now=1,L=0;return;}
  29. L=len[now]+1;now=son[now][c];ans=max(ans,L);
  30. }
  31. int main()
  32. {
  33. scanf("%s",S+1);n=strlen(S+1);
  34. for(int i=1;i<=n;i++) ins((int)(S[i]-'a'));
  35. scanf("%s",T+1);n=strlen(T+1);
  36. for(int i=1;i<=n;i++) q((int)(T[i]-'a'));
  37. printf("%d\n",ans);
  38. return 0;
  39. }

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

  1. 【题解】SP10570 【LONGCS - Longest Common Substring】

    \(\color{Red}{Link}\) \(\text{Solution:}\) 还是\(\text{Suffix Tree.}\) 根据\(\color{Blue}{Link}\)我们可以得到一 ...

  2. 【SP1811】LCS - Longest Common Substring

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

  3. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

  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 LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  8. 【刷题】SPOJ 1811 LCS - Longest Common Substring

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

  9. SPOJ 1811 LCS - Longest Common Substring

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

随机推荐

  1. git——合并分支

    A将自己的本地代码提交到远程A分支,此时master主干上有B新提交的代码,如果此时A把自己的代码merge到主干,会有冲突,那怎么办? 1.A将自己的代码提交到自己的A分支 2.git fetch ...

  2. 设计模式学习总结(五)创建者模式(Builder)

    创建者模式,主要针对某些产品有类似的生产步骤,且有需要有先后顺序的进行各个部件的生成. 一.示例展示: 通过学习及总结,以下是我完成的创建者模式的示例: 1.创建产品类:Laptop public c ...

  3. 解决IE9 IE8的跨域 请求问题

    /// <summary> /// 根据url获取对应的HTML /// </summary> /// <param name="url">&l ...

  4. MySQL关联left join 条件on与where不同

    以下的文章主要讲述的是MySQL关联left join 条件on与where 条件的不同之处,我们现在有两个表,即商品表(products)与sales_detail(销售记录表).我们主要是通过这两 ...

  5. [html/js]点击标题出现下拉列表

    效果 初始 点击后 参考代码 <!DOCTYPE html> <html> <head> <title>Layer group example</ ...

  6. Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  7. [转]Tomcat优化之内存、并发、缓存

    1.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 JAVA_OPTS 参数. JAVA_OPTS ...

  8. css 小常识

    一.vertical-align可以采用负值(正/负值根据基线上下移动),也可以采用百分比值,而这个百分比值不是相对于字体大小或者其他什么属性计算的,而是相对于line-height计算的. 此外,w ...

  9. GridCellChoiceEditor

    choice_editor = wx.grid.GridCellChoiceEditor(choices_list, True) grid.SetCellEditor(row, col, choice ...

  10. python 正则,os,sys,hashlib模块

    简单的小算法 random随机获取数据 import random def getrandata(num): a=[] i= while i<num: a.append(random.randi ...