HDU 1711 Number Sequence(KMP模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1711

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define INF 0x3f3f3f3f
  5. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  6. #define pqueue priority_queue
  7. #define NEW(a,b) memset(a,b,sizeof(a))
  8. const double pi=4.0*atan(1.0);
  9. const double e=exp(1.0);
  10. const int maxn=1e6+;
  11. typedef long long LL;
  12. typedef unsigned long long ULL;
  13. //typedef pair<LL,LL> P;
  14. const LL mod=1e9+;
  15. using namespace std;
  16. int a[maxn],b[maxn],Next[maxn];
  17. int n,m;
  18. void get_next(){
  19. Next[]=-;
  20. int k=-,i=;
  21. while(i<m){
  22. while(k>-&&b[k]!=b[i]){
  23. k=Next[k];
  24. }
  25. if(b[k]==b[i]||k==-){
  26. k++;
  27. }
  28. Next[++i]=k;
  29. }
  30. return;
  31. }
  32. int main(){
  33. fio;
  34. int t;
  35. cin>>t;
  36. while(t--){
  37. cin>>n>>m;
  38. for(int i=;i<n;i++){
  39. cin>>a[i];
  40. }
  41. for(int i=;i<m;i++){
  42. cin>>b[i];
  43. }
  44. get_next();
  45. int p=;
  46. int is=-;
  47. for(int i=;i<n;){
  48. if(a[i]==b[p]||p==-){
  49. p++;
  50. i++;
  51. }
  52. else{
  53. while(p!=-){
  54. if(b[p]==a[i]){
  55. break;
  56. }
  57. p=Next[p];
  58.  
  59. }
  60. }
  61. if(p==m){
  62. is=i-m+;
  63. break;
  64. }
  65. }
  66. cout<<is<<endl;
  67. }
  68. }

HDU 2222 Keywords Search(AC自动机模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=2222

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define INF 0x3f3f3f3f
  5. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  6. #define pqueue priority_queue
  7. #define NEW(a,b) memset(a,b,sizeof(a))
  8. const double pi=4.0*atan(1.0);
  9. const double e=exp(1.0);
  10. const int maxn=1e6+;
  11. typedef long long LL;
  12. typedef unsigned long long ULL;
  13. //typedef pair<LL,LL> P;
  14. const LL mod=1e9+;
  15. using namespace std;
  16. struct node{
  17. node *nxt[];
  18. node *fail;
  19. int is;
  20. void init(){
  21. is=;
  22. fail=;
  23. NEW(nxt,);
  24. }
  25. };
  26. node *root;
  27. node *q[maxn];
  28. int cnt;
  29. void Insert(string s){
  30. node *p=root;
  31. int x;
  32. for(int i=;s[i];i++){
  33. x=s[i]-'a';
  34. if(p->nxt[x]==NULL){
  35. p->nxt[x]=new node;
  36. p->nxt[x]->init();
  37. }
  38. p=p->nxt[x];
  39. }
  40. p->is++;
  41. }
  42. void build_failpoint(){
  43. int head=;
  44. int tail=;
  45. q[]=root;
  46. node *tmp;
  47. node *f;
  48. while(head<tail){
  49. tmp=q[head++];
  50. for(int i=;i<;i++){
  51. if(tmp->nxt[i]){
  52. if(tmp==root){
  53. tmp->nxt[i]->fail=root;
  54. }
  55. else{
  56. f=tmp->fail;
  57. while(f!=){
  58. if(f->nxt[i]!=){
  59. tmp->nxt[i]->fail=f->nxt[i];
  60. break;
  61. }
  62. f=f->fail;
  63. }
  64. if(f==){
  65. tmp->nxt[i]->fail=root;
  66. }
  67. }
  68. q[tail++]=tmp->nxt[i];
  69. }
  70. }
  71. }
  72. }
  73. void ac_automation(string t){
  74. node *p=root;
  75. int x;
  76. for(int i=;t[i];i++){
  77. x=t[i]-'a';
  78. if(p->nxt[x]){
  79. p=p->nxt[x];
  80. }
  81. else{
  82. p=p->fail;
  83. while(p!=){
  84. if(p->nxt[x]){
  85. p=p->nxt[x];
  86. break;
  87. }
  88. p=p->fail;
  89. }
  90. if(p==){
  91. p=root;
  92. }
  93. }
  94. node *tmp=p;
  95. while(tmp->is!=-&&tmp!=root){
  96. cnt+=tmp->is;
  97. tmp->is=-;
  98. tmp=tmp->fail;
  99. }
  100. }
  101. }
  102. int main(){
  103. fio;
  104. int t;
  105. int n;
  106. cin>>t;
  107. string s;
  108. while(t--){
  109. cin>>n;
  110. cnt=;
  111. root=new node;
  112. root->init();
  113. for(int i=;i<n;i++){
  114. cin>>s;
  115. Insert(s);
  116. }
  117. build_failpoint();
  118. cin>>s;
  119. ac_automation(s);
  120. cout<<cnt<<endl;
  121. }
  122. }

AC自动机改成了数组版(表示作为下标党用不惯指针)

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define INF 0x3f3f3f3f
  5. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  6. #define pqueue priority_queue
  7. #define NEW(a,b) memset(a,b,sizeof(a))
  8. const double pi=4.0*atan(1.0);
  9. const double e=exp(1.0);
  10. const int maxn=1e6+;
  11. typedef long long LL;
  12. typedef unsigned long long ULL;
  13. //typedef pair<LL,LL> P;
  14. const LL mod=1e9+;
  15. using namespace std;
  16. struct node{
  17. int nxt[];
  18. int fail;
  19. int is;
  20. void init(){
  21. is=;
  22. fail=;
  23. NEW(nxt,);
  24. }
  25. }q[maxn];
  26. int root=,tot=;
  27. int cnt,que[maxn];
  28. void Insert(string s){
  29. int p=root;
  30. int x;
  31. for(int i=;s[i];i++){
  32. x=s[i]-'a';
  33. if(q[p].nxt[x]==){
  34. q[p].nxt[x]=++tot;
  35. q[q[p].nxt[x]].init();
  36. }
  37. p=q[p].nxt[x];
  38. }
  39. q[p].is++;
  40. }
  41. void build_failpoint(){
  42. int head=;
  43. int tail=;
  44. que[]=root;
  45. int tmp;
  46. int f;
  47. while(head<tail){
  48. tmp=que[head++];
  49. for(int i=;i<;i++){
  50. if(q[tmp].nxt[i]){
  51. if(tmp==root){
  52. q[q[tmp].nxt[i]].fail=root;
  53. }
  54. else{
  55. f=q[tmp].fail;
  56. while(f!=){
  57. if(q[f].nxt[i]!=){
  58. q[q[tmp].nxt[i]].fail=q[f].nxt[i];
  59. break;
  60. }
  61. f=q[f].fail;
  62. }
  63. if(f==){
  64. q[q[tmp].nxt[i]].fail=root;
  65. }
  66. }
  67. que[tail++]=q[tmp].nxt[i];
  68. }
  69. }
  70. }
  71. }
  72. void ac_automation(string t){
  73. int p=root;
  74. int x;
  75. for(int i=;t[i];i++){
  76. x=t[i]-'a';
  77. if(q[p].nxt[x]){
  78. p=q[p].nxt[x];
  79. }
  80. else{
  81. p=q[p].fail;
  82. while(p!=){
  83. if(q[p].nxt[x]){
  84. p=q[p].nxt[x];
  85. break;
  86. }
  87. p=q[p].fail;
  88. }
  89. if(p==){
  90. p=root;
  91. }
  92. }
  93. int tmp=p;
  94. while(q[tmp].is!=-&&tmp!=root){
  95. cnt+=q[tmp].is;
  96. q[tmp].is=-;
  97. tmp=q[tmp].fail;
  98. }
  99. }
  100. }
  101. int main(){
  102. fio;
  103. int t;
  104. int n;
  105. cin>>t;
  106. string s;
  107. while(t--){
  108. cin>>n;
  109. cnt=;
  110. q[root].init();
  111. for(int i=;i<n;i++){
  112. cin>>s;
  113. Insert(s);
  114. }
  115. build_failpoint();
  116. cin>>s;
  117. ac_automation(s);
  118. cout<<cnt<<endl;
  119. }
  120. }

KMP与AC自动机模板的更多相关文章

  1. HDU:2222-Keywords Search(AC自动机模板,匹配模拟)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) P ...

  2. KMP,Trie,AC自动机题目集

    字符串算法并不多,KMP,trie,AC自动机就是其中几个最经典的.字符串的题目灵活多变也有许多套路,需要多做题才能体会.这里收集了许多前辈的题目做个集合,方便自己回忆. KMP题目:https:// ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  5. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  6. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  7. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  8. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  9. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

随机推荐

  1. JVM总结-java内存模型

    我们先来看一个反常识的例子. int a=0, b=0; public void method1() { int r2 = a; b = 1; } public void method2() { in ...

  2. mac中svn服务器的搭建以及如何在eclipse中使用

    mac自带了svn客户端和服务端功能. 1.查看svn版本 svnserve --version yintingtingdeMacBook-Pro:~ yintingting$ svnserve -- ...

  3. python - 配置文件

    #配置文件 #.ini .properties .conf 等都是配置文件 #section 片段[]: option 选项 #同一个section下option都是唯一的 #语法 #[secion] ...

  4. 2、申请苹果App ID

    在“iOS Certificates”页面“Identifiers"下选择“App IDs",可查看到已申请的所有App ID,点击右上角的加号可创建新“App ID”:打开“Re ...

  5. JAVA 操作mysql 存储 调用

    HashMap map = new HashMap(); map.put("a", 9); map.put("b", ""); List&l ...

  6. C语言学习一个月后感想

    C语言学习一个月后感想 感谢李晓东老板及计算机工程师联盟的学长学姐和某神秘同级同学的辛勤指导,感谢宋雨田的督促和陪伴. 初识C的1..体会 我本以为凭借瓜皮思维和花里胡哨操作可以让我熟练地学习语言,现 ...

  7. nginx 服务器 在 centos7 系统下的两种方式

    选用系统 Centos7 < 一 >  使用 yum 安装,该方法比较方便,如果不追求版本推荐使用 01, 添加 nginx 储存库  yum -y install epel-releas ...

  8. kubernets之endpoints

    注:本文整理自网络 endpoint endpoint是k8s集群中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址.service配置selector,endp ...

  9. leetcode989

    class Solution: def addToArrayForm(self, A, K): i = len(A) - 1 while i >= 0 and K > 0: A[i] += ...

  10. react-navigation,StackNavigator,TabNavigator 导航使用

    StackNavigator  参考这里 TabNavigator  参考这里 是一个组合,我把这2个写在一起了 代码: import React, { Component } from 'react ...