HDU 2896 病毒侵袭 AC自动机
来源:互联网 发布:吾生也有涯 知也无涯 编辑:程序博客网 时间:2024/06/06 01:56
题意:
中文题
思路:
不同的子串打上各自的id,跑匹配的时候统计一下就行。注意一个子串可能会多次出现在母串中
代码:
#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <queue>using namespace std;struct Trie{ int next[150000][128],fail[150000],end[150000]; 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 buf[],int id) { int len = strlen(buf); int now = root; for(int i = 0; i < len; i++) { if(next[now][buf[i]] == -1) next[now][buf[i]] = newnode(); now = next[now][buf[i]]; } end[now] = id; } void build() { queue<int>Q; 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; Q.push(next[root][i]); } while( !Q.empty() ) { int now = Q.front(); Q.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]; Q.push(next[now][i]); } } } int query(char buf[],int id) { int res[4]; bool mark[510] = {0}; int len = strlen(buf); int now = root; memset(res,0,sizeof(res)); for(int i = 0; i < len; i++) { now = next[now][buf[i]]; int temp = now; while( temp != root ) { if(end[temp] != -1){ if(!mark[end[temp]]){ res[++res[0]] = end[temp]; mark[end[temp]] = 1; } } temp = fail[temp]; } } if(res[0]){ printf("web %d: ",id); sort(res+1,res+res[0]+1); for(int i = 1;i <= res[0];i++){ if(i == res[0]) printf("%d\n",res[i]); else printf("%d ",res[i]); } return 1; } return 0; }};char buf[20000];Trie ac;int main(){ int n,m; while(~scanf("%d",&n)){ ac.init(); for(int i = 0; i < n; i++) { scanf("%s",buf); ac.insert(buf,i+1); } ac.build(); scanf("%d",&m); int tot = 0; for(int i = 0;i < m;i++){ //test = ac; scanf("%s",buf); tot+=ac.query(buf,i+1); } printf("total: %d\n",tot); } 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自动机)
- 使用AJAX技术做一个无页面刷新的会员登录功能--解决中文乱码
- CortexM3,CortexM4存储器映像
- 查看MySql时间函数:DATE_SUB、DATE_ADD、UNIX_TIMESTAMP等函数的用法
- ActionBar简单实用
- 带你玩转Visual Studio——带你理解微软的预编译头技术
- HDU 2896 病毒侵袭 AC自动机
- 读 给 Android 开发者的 RxJava 详解 笔记
- 关于3D touch 一些简单实用
- neutron availability zones
- 中文API学习文档(持续更新)
- Android 数据库打包随APK发布
- Kali Linux 网络扫描秘籍 第三章 端口扫描(一)
- Activity的滑动返回效果
- Oracle中的事务之savepoint