hdu2222(AC自动机入门)
来源:互联网 发布:西南大学网络教育如何 编辑:程序博客网 时间:2024/05/21 01:44
http://acm.hdu.edu.cn/showproblem.php?pid=2222
AC自动机--------字典树+KMP原理
挺有趣的。
#include <stdio.h>#include <string.h>#include <math.h>#include <queue>#include<iostream>using namespace std;#define N 500010#define MAXN 1000010char str[MAXN],keyword[51];int head,tail;struct node{ node *fail; node *next[26]; int cnt; node(){ fail=NULL; cnt=0; for(int i=0;i<26;i++) next[i]=NULL; }}*q[N];node *root;void insert(char *str){//建树 int temp,len; node *p=root; len = strlen(str); for(int i=0;i<len;i++){ temp=str[i]-'a'; if(p->next[temp]==NULL) p->next[temp]=new node(); p=p->next[temp];//printf("1?\n"); }//for p->cnt++;}//insertvoid build_ac(){//初始化fail指针,BFS q[tail++]=root; while(head!=tail){ node *p=q[head++];//弹出队头 node *temp=NULL; for(int i=0;i<26;i++){ if(p->next[i]!=NULL){ if(p==root) p->next[i]->fail=root;//第一个元素fail必指向根 else{ temp = p->fail;//失败指针 while(temp!=NULL){ if(temp->next[i]!=NULL){p->next[i]->fail=temp->next[i];break;}//找到匹配 temp=temp->fail; }//while if(temp==NULL) p->next[i]->fail=root;//为空则从头匹配 }//else q[tail++] =p->next[i];//入列 }//if }//for }//while}//build_acint query(){ int index, len, res; node *p=root;//Tire入口 res=0;//ext len=strlen(str); for(int i=0;i<len;i++){ index = str[i]-'a'; while(p->next[index]==NULL&&p!=root) p=p->fail;//跳转失败指针 p=p->next[index]; if(p==NULL) p=root; node *temp=p;//p不动,temp计算缀串 while(temp != root && temp->cnt!=-1){ res+=temp->cnt; temp->cnt=-1; temp=temp->fail; }//while }//for return res;}//queryint main(){ int T,num; scanf("%d",&T); while(T--){ head=tail=0; root = new node(); scanf("%d",&num); getchar(); for(int i=0;i<num;i++){ gets(keyword); insert(keyword); }//for build_ac(); scanf("%s",str); printf("%d\n",query()); }//while return 0;}
0 0
- HDU2222 AC自动机入门
- hdu2222(AC自动机入门)
- hdu2222之AC自动机入门
- HDU2222 AC自动机 入门模板
- hdu2222 AC自动机入门 指针型模板
- AC自动机入门——HDU2222
- HDU2222 Keywords Search AC自动机入门
- hdu2222-- Keywords Search(AC自动机入门1)
- HDU2222 Keywords Search(AC自动机入门)
- AC自动机入门详解+例题 hdu2222
- HDU2222(AC自动机入门题)
- AC自动机的初步学习 hdu2222 AC自动机入门题
- hdu2222 ac自动机模版
- hdu2222 AC自动机
- hdu2222,(ac自动机)
- HDU2222-tire,AC自动机
- AC自动机模板 hdu2222
- hdu2222-AC自动机
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- Windows 右键相关:不用按Shift,直接显示“在此处打开命令窗口 ”
- 求int型数据在内存中存储时1的个数
- Unity shader学习资料
- Spring和MyBatis学习 (卷首)
- hdu2222(AC自动机入门)
- 面试题4
- Failed to load class "org.slf4j.impl.StaticLoggerBinder问题故障的解决
- 基金公司官方网站设计建设
- Xcode清除多余的Provisioning Profile
- 黑马程序员——Set实现类、Collections工具类
- scala-04For与Function进阶实战、Lazy的使用
- Codeforces Round #315-Codeforces Round #315(贝尔数)
- 设置DIV为不可见