【BZOJ4032】[HEOI2015]最短不公共子串(后缀自动机,序列自动机)

题面

BZOJ

洛谷

题解

数据范围很小,直接暴力构建后缀自动机和序列自动机,然后直接在两个自动机上进行\(bfs\),找到的第一个不同时存在的节点就直接输出就好了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. #define MAX 4040
  7. struct SAM
  8. {
  9. struct Node{int son[26],ff,len;}t[MAX];
  10. int tot,last;void init(){tot=last=1;}
  11. void extend(int c)
  12. {
  13. int p=last,np=++tot;last=tot;
  14. t[np].len=t[p].len+1;
  15. while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
  16. if(!p)t[np].ff=1;
  17. else
  18. {
  19. int q=t[p].son[c];
  20. if(t[q].len==t[p].len+1)t[np].ff=q;
  21. else
  22. {
  23. int nq=++tot;
  24. t[nq]=t[q];t[nq].len=t[p].len+1;
  25. t[q].ff=t[np].ff=nq;
  26. while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
  27. }
  28. }
  29. }
  30. }A,B;
  31. struct SQAM
  32. {
  33. struct Node{int son[26];}t[MAX];
  34. int lst[26],pre[MAX],tot,last;
  35. void init(){tot=last=1;for(int i=0;i<26;++i)lst[i]=1;}
  36. void extend(int c)
  37. {
  38. int p=lst[c],np=++tot;
  39. pre[np]=p;
  40. for(int i=0;i<26;++i)
  41. for(int j=lst[i];j&&!t[j].son[c];j=pre[j])
  42. t[j].son[c]=np;
  43. lst[c]=np;
  44. }
  45. }AA,BB;
  46. char SA[MAX],SB[MAX];
  47. int na,nb;
  48. struct Node{int len,a,b;};
  49. bool vis[MAX][MAX];
  50. int bfs1(SAM &A,SAM &B)
  51. {
  52. queue<Node> Q;Q.push((Node){0,1,1});
  53. memset(vis,0,sizeof(vis));vis[1][1]=true;
  54. while(!Q.empty())
  55. {
  56. Node p=Q.front();Q.pop();
  57. for(int i=0;i<26;++i)
  58. {
  59. int x=A.t[p.a].son[i];
  60. int y=B.t[p.b].son[i];
  61. if(x&&y)
  62. {
  63. if(vis[x][y])continue;
  64. vis[x][y]=true;Q.push((Node){p.len+1,x,y});
  65. }
  66. if(x&&!y)return p.len+1;
  67. }
  68. }
  69. return -1;
  70. }
  71. int bfs2(SAM &A,SQAM &B)
  72. {
  73. queue<Node> Q;Q.push((Node){0,1,1});
  74. memset(vis,0,sizeof(vis));vis[1][1]=true;
  75. while(!Q.empty())
  76. {
  77. Node p=Q.front();Q.pop();
  78. for(int i=0;i<26;++i)
  79. {
  80. int x=A.t[p.a].son[i];
  81. int y=B.t[p.b].son[i];
  82. if(x&&y)
  83. {
  84. if(vis[x][y])continue;
  85. vis[x][y]=true;Q.push((Node){p.len+1,x,y});
  86. }
  87. if(x&&!y)return p.len+1;
  88. }
  89. }
  90. return -1;
  91. }
  92. int bfs3(SQAM &A,SAM &B)
  93. {
  94. queue<Node> Q;Q.push((Node){0,1,1});
  95. memset(vis,0,sizeof(vis));vis[1][1]=true;
  96. while(!Q.empty())
  97. {
  98. Node p=Q.front();Q.pop();
  99. for(int i=0;i<26;++i)
  100. {
  101. int x=A.t[p.a].son[i];
  102. int y=B.t[p.b].son[i];
  103. if(x&&y)
  104. {
  105. if(vis[x][y])continue;
  106. vis[x][y]=true;Q.push((Node){p.len+1,x,y});
  107. }
  108. if(x&&!y)return p.len+1;
  109. }
  110. }
  111. return -1;
  112. }
  113. int bfs4(SQAM &A,SQAM &B)
  114. {
  115. queue<Node> Q;Q.push((Node){0,1,1});
  116. memset(vis,0,sizeof(vis));vis[1][1]=true;
  117. while(!Q.empty())
  118. {
  119. Node p=Q.front();Q.pop();
  120. for(int i=0;i<26;++i)
  121. {
  122. int x=A.t[p.a].son[i];
  123. int y=B.t[p.b].son[i];
  124. if(x&&y)
  125. {
  126. if(vis[x][y])continue;
  127. vis[x][y]=true;Q.push((Node){p.len+1,x,y});
  128. }
  129. if(x&&!y)return p.len+1;
  130. }
  131. }
  132. return -1;
  133. }
  134. int main()
  135. {
  136. scanf("%s%s",SA+1,SB+1);
  137. na=strlen(SA+1);nb=strlen(SB+1);
  138. A.init();B.init();AA.init();BB.init();
  139. for(int i=1;i<=na;++i)A.extend(SA[i]-97);
  140. for(int i=1;i<=nb;++i)B.extend(SB[i]-97);
  141. for(int i=1;i<=na;++i)AA.extend(SA[i]-97);
  142. for(int i=1;i<=nb;++i)BB.extend(SB[i]-97);
  143. printf("%d\n%d\n%d\n%d\n",bfs1(A,B),bfs2(A,BB),bfs3(AA,B),bfs4(AA,BB));
  144. return 0;
  145. }

【BZOJ4032】[HEOI2015]最短不公共子串(后缀自动机,序列自动机)的更多相关文章

  1. bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp)

    bzoj4032/luoguP4112 [HEOI2015]最短不公共子串(后缀自动机+序列自动机上dp) bzoj Luogu 题解时间 给两个小写字母串 $ A $ , $ B $ ,请你计算: ...

  2. BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力

    4032: [HEOI2015]最短不公共子串 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4032 Description 在虐各种最 ...

  3. bzoj4032: [HEOI2015]最短不公共子串(SAM+DP)

    4032: [HEOI2015]最短不公共子串 题目:传送门 题解: 陈年老题良心%你赛膜爆嘎爷 当初做题...一眼SAM...结果只会两种直接DP的情况... 情况1: 直接设f[i][j] 表示的 ...

  4. BZOJ4032 [HEOI2015]最短不公共子串 【后缀自动机 + 序列自动机 + dp】

    题目链接 BZOJ4032 题解 首先膜\(hb\) 空手切神题 一问\(hash\),二问枚举 三问\(trie\)树,四问\(dp\) 南二巨佬神\(hb\) 空手吊打自动机 \(orz orz ...

  5. BZOJ4032[HEOI2015]最短不公共子串——序列自动机+后缀自动机+DP+贪心

    题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...

  6. BZOJ4032: [HEOI2015]最短不公共子串(后缀自动机+序列自动机)

    题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...

  7. [BZOJ4032][HEOI2015]最短不公共子串(Trie+DP)

    在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之——被它们虐. 操作一:对A,B分别建SAM,暴力BFS. 操作二:对B建序列自动机或SAM,A在上面暴力匹配. 操作三:对A,B建 ...

  8. BZOJ4032:[HEOI2015]最短不公共子串(SAM)

    Description 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列” ...

  9. bzoj 4032 [HEOI2015]最短不公共子串——后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4032 不是 b 的子串的话就对 b 建后缀自动机,在 a 上枚举从每个位置开始的子串或者找子 ...

  10. BZOJ 4032: [HEOI2015]最短不公共子串(后缀自动机+记忆化搜索)

    传送门 解题思路 首先需要预处理两个串\(nxt(i)(j)\)表示i位置之后最近的\(j\). 第一问直接对\(b\)建后缀自动机,枚举\(a\)的起点暴力匹配. 第二问枚举\(a\)的起点,\(b ...

随机推荐

  1. Raptor入门与安装

    作为计算机导论的一部分,Raptor的图形化界面可以让编程的初学者更加容易深入理解算法,可以作为一个简单入门的学习工具. 1.Raptor是什么? Raptor( the Rapid Algorith ...

  2. js基础复习点

    1.变量   var num=10;       var num1,num2,num3;   num1=10;   num2=20;   num3=30;       var num1=10,num2 ...

  3. mac webstorm无法打开 如何使webstorm不卡

    场景:在应用程序里删除了原先的webstorm,然后从官网下载了新的安装包,进行安装.安装后,webstorm就再也打不开了. 解决方案:执行以下命令,清楚webstorm所有缓存,然后重新安装 $ ...

  4. 演示Eclipse插件实现代码提示和补全

    续上文重拾< 两周自制脚本语言 >- Eclipse插件实现语法高亮, 但仅达到了演示Eclipse本身功能的程度, 与石头语言并无直接联系. 源码库相同, 仍在同一插件. 演示效果如下: ...

  5. [C#学习笔记3]关于Main(string[ ] args)中args命令行参数

    Main(string[] args)方法是C#程序的入口,程序从这里开始执行,在这里结束.C#代码逻辑要包含在一个类型(Type)中,游离的.全局的变量或函数是不存在的,这里的类型包括类(class ...

  6. web安全基础第一天

    编码:url编码 base64编码:末尾有俩个==号 md5加密:16位或者32位 1.whois查询  :站长邮箱,联系人,备案 旁站c段&子域名. ( 大数据平台和bing接口查询 :Zo ...

  7. 免费了 -- EXCEL插件 智表ZCELL 普及版V1.0 发布了!!!

    智表(zcell)是一款浏览器仿excel表格jquery插件.智表可以为你提供excel般的智能体验,支持双击编辑.设置公式.设置显示小数精度.下拉框.自定义单元格.复制粘贴.不连续选定.合并单元格 ...

  8. 【题解】P2922 [USACO08DEC]秘密消息Secret Message

    \(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...

  9. Even Parity UVA - 11464 (枚举)

    从来没有觉得枚举有多费脑子的.但是这道题还是很香的. 思路:就是非常简单的枚举啦.   从一般的枚举开始考虑.一般的做法就是在所有的格子中有两种状态1, 0. 而一共有225个格子,所有一共要枚举的情 ...

  10. node的第一步,hello,以及小技巧和CPU使用情况。到底能用几个核心?

    安装了啥的就不说了,百度一下有很多. Windows环境.Linux不会,所有就不说了. 1.  hello Word node的hello Word很简单,就一行. console.log(&quo ...