博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2222AC自动机模板题
阅读量:4204 次
发布时间:2019-05-26

本文共 1574 字,大约阅读时间需要 5 分钟。

题意:问前面给的所有串在最后一个串出现的次数

思路:这道题是AC自动机入门必做的题,所以没什么好说的,是个模版题,推荐一个大神写的详解,不懂得可以看一看,反正我是看他的稍稍懂了点

[html]   
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <iostream>  
  5. #include <algorithm>  
  6. using namespace std;  
  7. typedef long long ll;  
  8. const int inf=0x3f3f3f3f;  
  9. const int maxn=500010;  
  10. const int N=26;  
  11. struct node{  
  12.     node *fail;  
  13.     node *next[N];  
  14.     int num;  
  15.     node(){  
  16.         fail=NULL;  
  17.         num=0;  
  18.         memset(next,NULL,sizeof(next));  
  19.     }  
  20. }*q[maxn];  
  21. char str1[60],str2[1000010];  
  22. int head,tail;  
  23. void insert_Trie(char *str,node *root){  
  24.     node *p=root;  
  25.     int i=0;  
  26.     while(str[i]){  
  27.         int id=str[i]-'a';  
  28.         if(p->next[id]==NULL) p->next[id]=new node();  
  29.         p=p->next[id];i++;  
  30.     }  
  31.     p->num++;  
  32. }  
  33. void build_ac(node *root){  
  34.     root->fail=NULL;  
  35.     q[head++]=root;  
  36.     while(head!=tail){  
  37.         node *temp=q[tail++];  
  38.         node *p=NULL;  
  39.         for(int i=0;i<N;i++){  
  40.             if(temp->next[i]!=NULL){  
  41.                 if(temp==root) temp->next[i]->fail=root;  
  42.                 else{  
  43.                     p=temp->fail;  
  44.                     while(p!=NULL){  
  45.                         if(p->next[i]!=NULL){  
  46.                             temp->next[i]->fail=p->next[i];  
  47.                             break;  
  48.                         }  
  49.                         p=p->fail;  
  50.                     }  
  51.                     if(p==NULL) temp->next[i]->fail=root;  
  52.                 }  
  53.                 q[head++]=temp->next[i];  
  54.             }  
  55.         }  
  56.     }  
  57. }  
  58. int query(node *root){  
  59.     int i=0,ans=0;  
  60.     node *p=root;  
  61.     while(str2[i]){  
  62.         int id=str2[i]-'a';  
  63.         while(p->next[id]==NULL&&p!=root) p=p->fail;  
  64.         p=p->next[id];  
  65.         p=(p==NULL)?root:p;  
  66.         node *temp=p;  
  67.         while(temp!=root&&temp->num!=-1){  
  68.             ans+=temp->num;  
  69.             temp->num=-1;  
  70.             temp=temp->fail;  
  71.         }  
  72.         i++;  
  73.     }  
  74.     return ans;  
  75. }  
  76. int main(){  
  77.     int T,n;  
  78.     scanf("%d",&T);  
  79.     while(T--){  
  80.         node *root=new node();  
  81.         head=tail=0;  
  82.         scanf("%d",&n);  
  83.         for(int i=0;i<n;i++){  
  84.             scanf("%s",str1);  
  85.             insert_Trie(str1,root);  
  86.         }  
  87.         build_ac(root);  
  88.         scanf("%s",str2);  
  89.         int ans=query(root);  
  90.         printf("%d\n",ans);  
  91.     }  
  92.     return 0;  
  93. }  

转载地址:http://giali.baihongyu.com/

你可能感兴趣的文章
Mongodb 创建用户后登陆出现mongoAuthentication failed
查看>>
Mongodb GridFS、服务器脚本和数据库引用
查看>>
Mongodb 数据库管理
查看>>
JAVA 基本类型的默认值和取值范围
查看>>
JDK 1.5-1.8特性
查看>>
Jsp 出现异常IllegalArgumentException:Control character in cookie value or attribute解决方法
查看>>
Servlet 使用字符流读取文件乱码解决方法
查看>>
设计模式 Concurrency 之 ReadWriteLock 读写锁
查看>>
Spring 复习总结
查看>>
剑指Offer 二叉树的镜像
查看>>
剑指Offer 含有Min函数的栈
查看>>
Mybatis 主键配置
查看>>
JVM 参数使用总结
查看>>
剑指Offer 最小的K个数
查看>>
剑指Offer 调整数组顺序使奇数位于偶数前面
查看>>
剑指Offer SnakeNumber 蛇形填数
查看>>
剑指Offer TurnOnLight 开灯问题
查看>>
剑指Offer RotateArray 旋转数组的最小数字
查看>>
剑指Offer FindNumberMoreThanHalf 找出数组中出现次数超过一半的数字
查看>>
剑指Offer AccurateFactorial 计算精确的阶乘
查看>>