HDU 2222 Keywords Search (AC自动机)
来源:互联网 发布:glide加载网络图片 编辑:程序博客网 时间:2024/04/18 05:04
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2222
标准的AC自动机题目。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;typedef long long LL;const int N=1000005;const int mod=1000007;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);#define L 27 //表示字符串范围char str[N], keyword[51];int head,tail;struct node{ node *fail; node *next[L]; int count; node() //init内联 { fail = NULL; count = 0; for(int i = 0; i < L; ++i) next[i] = NULL; }}*q[N];node *root;void insert(char *str) //建立Trie树{ 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]; } p->count++;}void build_ac() //初始化fail指针,BFS{ q[tail++] = root; while(head != tail) { node *p = q[head++]; //弹出队头 node *temp = NULL; for(int i = 0; i < L; ++i) { if(p->next[i] != NULL) { if(p == root) //第一个元素fail必指向根 p->next[i]->fail = root; else { temp = p->fail; //失败指针 while(temp != NULL) //2种情况结束:匹配为空or找到匹配 { if(temp->next[i] != NULL) //找到匹配 { p->next[i]->fail = temp->next[i]; break; } temp = temp->fail; } if(temp == NULL) //为空则从头匹配 p->next[i]->fail = root; } q[tail++] = p->next[i]; //入队 } } }}int query() //扫描{ int index, len, result; node *p = root; //Tire入口 result = 0; 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->count != -1) { result += temp->count; temp->count = -1; temp = temp->fail; } } return result;}int main(){ int t,n,i; scanf("%d",&t); while(t--) { head= tail = 0; root = new node(); scanf("%d", &n); getchar(); for(i = 0; i < n; ++i) { gets(keyword); insert(keyword); } build_ac(); scanf("%s", str); printf("%d\n", query()); } return 0;}
- hdU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- hdu - 2222 - Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC 自动机)
- HDU 2222 - Keywords Search (AC自动机)
- HDU - 2222 Keywords Search (AC自动机)
- HDU - 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222Keywords Search (ac自动机)
- HDU 2222 Keywords Search(AC自动机)
- HDU-2222 Keywords Search(AC自动机)
- LinearLayout均分的分割线配置方式
- 22.从零开始学习C语言--链表
- FTP网络通信程序设计
- 关于box2d的缩放问题
- 【HDOJ】1097 -> A hard puzzle
- HDU 2222 Keywords Search (AC自动机)
- hdu3695(AC自动机)
- 苏州交通局长坐非空调公交满脸汗 决定提前换车
- as3遍历对象所有属性的方法
- 利用WebClient实现文件传送
- 比较ArrayList与一般数组
- 简单三步,学会Java Socket多线程编程
- 2_回家
- HDUOJ 4681 2013多校第8场第6题 String