【SPOJ】Longest Common Substring II (后缀自动机)

题面

Vjudge

题意:求若干个串的最长公共子串

题解

对于某一个串构建\(SAM\)

每个串依次进行匹配

同时记录\(f[i]\)表示走到了\(i\)节点

能够匹配上的最长公共子串的长度

当然,每个串的\(f[i]\)可以更新\(f[i.parent]\)

所以需要拓扑排序

对于每个串求出每个节点的最长匹配

然后对他们取\(min\),表示某个节点大家都能匹配的最长长度

最后对于所有点的值都取个\(max\)就是答案

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define MAX 120000
  13. struct Node
  14. {
  15. int son[26];
  16. int ff,len;
  17. }t[MAX<<1];
  18. char ch[MAX];
  19. int sum[MAX<<1],ans[MAX<<1];
  20. int last=1,tot=1;
  21. int c[MAX<<1],p[MAX<<1];
  22. void extend(int c)
  23. {
  24. int p=last,np=++tot;last=np;
  25. t[np].len=t[p].len+1;
  26. while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
  27. if(!p)t[np].ff=1;
  28. else
  29. {
  30. int q=t[p].son[c];
  31. if(t[q].len==t[p].len+1)t[np].ff=q;
  32. else
  33. {
  34. int nq=++tot;
  35. t[nq]=t[q];
  36. t[nq].len=t[p].len+1;
  37. t[q].ff=t[np].ff=nq;
  38. while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. scanf("%s",ch+1);
  45. for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-97);
  46. for(int i=1;i<=tot;++i)c[t[i].len]++;
  47. for(int i=1;i<=tot;++i)c[i]+=c[i-1];
  48. for(int i=1;i<=tot;++i)p[c[t[i].len]--]=i;
  49. for(int i=1;i<=tot;++i)ans[i]=t[i].len;
  50. while(scanf("%s",ch+1)!=EOF)
  51. {
  52. memset(sum,0,sizeof(sum));
  53. for(int i=1,l=strlen(ch+1),now=1,tt=0;i<=l;++i)
  54. {
  55. int c=ch[i]-97;
  56. if(t[now].son[c])++tt,now=t[now].son[c];
  57. else
  58. {
  59. while(now&&!t[now].son[c])now=t[now].ff;
  60. if(!now)tt=0,now=1;
  61. else tt=t[now].len+1,now=t[now].son[c];
  62. }
  63. sum[now]=max(sum[now],tt);
  64. }
  65. for(int i=tot;i;--i)sum[t[p[i]].ff]=max(sum[t[p[i]].ff],sum[p[i]]);
  66. for(int i=1;i<=tot;++i)ans[i]=min(ans[i],sum[i]);
  67. }
  68. int Ans=0;
  69. for(int i=1;i<=tot;++i)Ans=max(Ans,ans[i]);
  70. printf("%d\n",Ans);
  71. return 0;
  72. }

【SPOJ】Longest Common Substring II (后缀自动机)的更多相关文章

  1. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...

  2. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

  3. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

  4. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  5. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  8. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  9. spoj1812-Longest Common Substring II(后缀自动机)

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

  10. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

随机推荐

  1. [bzoj4551][Tjoi2016&Heoi2016]树-树链剖分

    Brief Description 给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个 结点,可以打多次标记.) ...

  2. 读书简记-java与模式

  3. Xen的虚拟化详解

    最近在看Xen在2003年发表在sosp上的论文<Xen and the Art of Virtualization>,中途遇到一些不理解的技术点,在网络上查找相关资料,发现大多数人都只是 ...

  4. 搭建ssr服务器

    搭建ssr服务器 首先,先说一下,为什么这么久没写博客. 一方面,最近在搭建自己的服务器.挺忙的. 另一方面,写了许多有关服务器构建,网站构建的word.但没有润色,所以打算等自己服务器做好了整理一下 ...

  5. bzoj 3622 已经没有什么好害怕的了 类似容斥,dp

    3622: 已经没有什么好害怕的了 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1213  Solved: 576[Submit][Status][ ...

  6. 通过js区分移动端浏览器(微信浏览器、QQ浏览器、QQ内置浏览器)

    由于公司业务中涉及到一个分享指引功能,而像微信.QQ内置浏览器需要引导用户点击右上角进行操作,其他浏览器则引导点击浏览器下方进行操作,因此需要区分浏览器类型: 通过在页面alert(navigator ...

  7. AJAX跨域完全讲解

    AJAX跨域完全讲解 今天在慕课网上学习了AJAX跨域完全讲解:https://www.imooc.com/learn/947 我在收集AJAX面试题的时候其实就已经有过AJAX跨域的问题的了,当时候 ...

  8. Enum枚举写的一个简单状态机

    今天下雨,心情有点压抑,所以用枚举写个状态机排解一下心情,顺便记录一下枚举使用方法. package statemachine; import java.util.ArrayList; import ...

  9. 谈一谈Java中的Error和Exception

    Error和Exception的联系 继承结构:Error和Exception都是继承于Throwable,RuntimeException继承自Exception. Error和RuntimeExc ...

  10. hiredis的安装

    Hiredis客户端下载地址:https://github.com/antirez/hiredis/zipball/master Hiredis安装步骤: tar zxvf antirez-hired ...