AC自动机 ( 模板题啊 )——病毒侵袭持续中 ( HDU 3065 )
2016-07-29 19:02
645 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3065
分析&&题解:
这持续侵袭的病毒还不如第一次病毒难Q^Q,直接模板改改就好了
AC代码:
http://acm.hdu.edu.cn/showproblem.php?pid=3065
分析&&题解:
这持续侵袭的病毒还不如第一次病毒难Q^Q,直接模板改改就好了
AC代码:
#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <cctype> #include <map> #include <set> #include <queue> using namespace std; typedef pair<int,int> Pii; typedef long long LL; typedef unsigned long long ULL; typedef double DBL; typedef long double LDBL; #define MST(a,b) memset(a,b,sizeof(a)) #define CLR(a) MST(a,0) #define Sqr(a) ((a)*(a)) const int k = 128; const int MAXN = 500100; int loc; int ans[1234]; struct Node { Node* ch[k], *fail; int match; void clear() { memset(this, 0, sizeof(Node)); } }; Node * que[MAXN]; struct ACAutomaton { Node nodes[MAXN], *root, *superRoot, *cur; //全局变量 Node * newNode() //从内存池中初始化一个结点 { cur -> clear(); return cur++; } void clear() //清空整个字典树 { cur = nodes; superRoot = newNode(); root = newNode(); root -> fail = superRoot; for(int i=0;i<k;i++) //superRoot为虚拟的超级根结点,所有孩子均指向实际的根结点,减少建立自动机的代码量 superRoot -> ch[i] = root; superRoot->match = -1; } void insert(char *s)//插入每一个字符,match++ { Node * t = root; for(;*s;s++) { int x = *s ; if(t -> ch[x] == NULL) t -> ch[x] = newNode(); t = t -> ch[x]; } t -> match = loc++; } void build() //使用自动机前,要先生成失配指针 { int p=0, q =0; que[q++] = root; while(p!=q) //BFS求失配指针 { Node*t = que[p++]; for(int i=0;i<k;i++) { if(t->ch[i]) { t -> ch[i] -> fail = t->fail ->ch[i]; que[q++] = t->ch[i]; }else t->ch[i] = t -> fail ->ch[i]; } } } void run(char *s) //计算s中模式串出现的次数 { CLR(ans); Node * t = root; for(; *s ; s++) { int x = *s ; t = t->ch[x]; for(Node*u = t; u->match != -1;u = u->fail) { int tmp = u ->match; ans[tmp]++; //u -> match = -1; //避免重复计算 } } } }; ACAutomaton j; string virus [1234]; char s[2000010]; int N; int main() { while(~scanf("%d", &N)) { j.clear(); loc = 1; for(int i=1;i<=N;i++) { scanf("%s", &s ); virus[i] = s; j.insert(s); } j.build(); scanf("%s", &s); j.run(s); for(int i=1;i<=N;i++) { if(ans[i]) { cout << virus[i] << ": " << ans[i] << endl; } } } return 0; }
相关文章推荐
- [HDU 3065]病毒侵袭持续中[AC自动机][模板题]
- hdu 3065 病毒侵袭持续中 ac自动机模板题
- hdu 3065 病毒侵袭持续中 AC自动机模板题 ,,一A。
- 病毒侵袭持续中 - HDU 3065(AC自动机,判断子串个数)
- HDU-3065 病毒侵袭持续中 AC自动机
- HDU-3065 病毒侵袭持续中 AC自动机!
- hdu 3065 病毒侵袭持续中 AC自动机模版题
- hdu 3065 病毒侵袭持续中 ac自动机
- 病毒侵袭持续中 - HDU 3065 AC自动机
- HDU 3065 病毒侵袭持续中 (AC自动机 + hdu有毒)
- [hdu 3065] 病毒侵袭持续中 [AC自动机] [病毒特征码匹配]
- HDU-3065 病毒侵袭持续中 AC自动机
- 【AC自动机】 HDU 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中 ac自动机
- hdu-3065 病毒侵袭持续中 --------AC自动机
- HDU 3065 病毒侵袭持续中 AC自动机题解
- HDU 3065 病毒侵袭持续中 AC自动机
- HDU 3065 病毒侵袭持续中【AC自动机】
- HDU 3065 病毒侵袭持续中 ac自动机
- AC自动机 病毒侵袭持续中 HDU - 3065