hdu 2896 病毒侵袭(AC自动机)
来源:互联网 发布:crm node online 编辑:程序博客网 时间:2024/05/16 05:29
用指针的那个ac自动机的模板做了好几遍,都是内存超限,然后从网上找了个相同模板做的,也是mle,看样子应该是题目的数据更新了。所以就找了个数组模拟指针的模板做的。。。
链接:http://www.cnblogs.com/kuangbin/p/3157240.html
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespace std;struct Trie{ int next[210*500][128],fail[210*500],end[210*500]; int root,L; int newnode() { for(int i = 0;i < 128;i++) next[L][i] = -1; end[L++] = -1; return L-1; } void init() { L = 0; root = newnode(); } void insert(char s[],int id) { int len = strlen(s); int now = root; for(int i = 0;i < len;i++) { if(next[now][s[i]] == -1) next[now][s[i]] = newnode(); now=next[now][s[i]]; } end[now]=id; } void buildAc() { queue<int> que; fail[root] = root; for(int i = 0;i < 128;i++) if(next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; que.push(next[root][i]); } while(!que.empty()) { int now = que.front(); que.pop(); for(int i = 0;i < 128;i++) if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; else { fail[next[now][i]] = next[fail[now]][i]; que.push(next[now][i]); } } } int res[510],glen; bool query(char buf[],int n,int id) { glen = 0; int len = strlen(buf); int now = root; bool flag = false; for(int i = 0;i < len;i++) { now = next[now][buf[i]]; int temp = now; while(temp != root) { if(end[temp] != -1) { res[glen++] = end[temp]; flag = true; } temp = fail[temp]; } } if(!flag)return false; printf("web %d:",id); sort(res,res+glen); for(int i = 0; i < glen; ++i) printf(" %d",res[i]); printf("\n"); return true; }};char buf[10010];Trie ac;int main(){ //freopen("in.txt","r",stdin); int n,m; while(scanf("%d",&n) != EOF) { ac.init(); for(int i = 1;i <= n;i++) { scanf("%s",buf); ac.insert(buf,i); } ac.buildAc(); int ans = 0; scanf("%d",&m); for(int i = 1;i <= m;i++) { scanf("%s",buf); if(ac.query(buf,n,i)) ans++; } printf("total: %d\n",ans); } return 0;}
0 0
- HDU 2896 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭 //AC自动机
- HDU 2896:病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 AC自动机
- HDU 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 AC自动机基础
- HDU 2896 病毒侵袭 AC自动机
- hdu 2896 病毒侵袭 -- AC自动机
- hdu 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭(AC 自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭 【AC自动机】
- hdu 2896 病毒侵袭 AC自动机
- [HDU 2896]病毒侵袭[AC自动机]
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- python的logging使用
- try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
- 申请免费startcom的ssl证书/CA证书
- oracle学习笔记 存储结构_表空间
- 七款出色的Python IDE
- hdu 2896 病毒侵袭(AC自动机)
- bzoj 1668
- Docker for Mac 安装及Mysql安装使用
- Java中字符和byte数组之间的相互转换
- NodeJS-基础入门(一)
- static
- 【HTML】淘宝注册页面
- c# DataView.ToTable() 方法 去除表中的重复项
- 高性能MySQL笔记之数据类型和scheme优化