http://acm.hdu.edu.cn/showproblem.php?pid=2896

题意:中文题意。

思路:AC自动机模板题。主要在于字符有128种,输出还要排序和去重!

注意是“total”不是“totol”!!!因为这个Debug了好久。

还有结点是new的,不然MLE。

主要用来测试模板,看了两个,发现没有注释掉的效率高点。

 #include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define N 100010
#define TOL 128 typedef struct Node {
Node* next[TOL];
Node* fail;
int id;
Node() {
for(int i = ; i < TOL; i++) next[i] = NULL;
fail = NULL; id = ;
}
} node; class AC_DFA { private:
int ans;
node *root; public:
AC_DFA() {
ans = ; root = new Node();
} void insert(char* s, int id) {
node* now = root;
int len = strlen(s);
for(int i = ; i < len; i++) {
char c = s[i];
if(now->next[c] == NULL) now->next[c] = new Node();
now = now->next[c];
}
now->id = id;
} void build() {
root->fail = NULL;
queue<node*> que;
que.push(root);
while(!que.empty()) {
node* now = que.front(); que.pop();
for(int i = ; i < TOL; i++) {
if(now->next[i]) {
node* p = now->fail;
while(p && p->next[i] == NULL) p = p->fail;
if(p) now->next[i]->fail = p->next[i];
else now->next[i]->fail = root;
que.push(now->next[i]);
} else {
if(now == root) now->next[i] = root;
else now->next[i] = now->fail->next[i];
}
}
}
} void match(char *s, int id) {
bool flag = ;
int len = strlen(s);
vector<int> tol;
node* now = root; node* p;
for(int i = ; i < len; i++) {
char c = s[i];
while(now->next[c] == NULL && now != root) now = now->fail;
now = now->next[c];
p = now;
while(p) {
if(p->id) tol.push_back(p->id), flag = ;
p = p->fail;
}
/*
char c = s[i];
now = now->next[c];
p = now;
while(p) {
if(p->id) tol.push_back(p->id), flag = 1;
p = p->fail;
}
*/
} if(!flag) return ;
sort(tol.begin(), tol.end());
int cnt = unique(tol.begin(), tol.end()) - tol.begin(); // 去重
ans++;
printf("web %d: ", id);
for(int i = ; i < cnt; i++) {
printf("%d", tol[i]);
if(i == cnt - ) putchar('\n');
else putchar(' ');
}
} void print() {
printf("total: %d\n", ans); // 不是totol
} }; int main() { AC_DFA ac;
char s[];
int n; scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%s", s); ac.insert(s, i);
}
ac.build();
int m; scanf("%d", &m);
for(int i = ; i <= m; i++) {
scanf("%s", s); ac.match(s, i);
}
ac.print();
return ;
}

HDU 2896:病毒侵袭(AC自动机)的更多相关文章

  1. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  2. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. hdu 2896 病毒侵袭 AC自动机 基础题

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  5. hdu 2896 病毒侵袭_ac自动机

    题意:略 思路:套用ac自动机模板 #include <iostream> #include<cstdio> #include<cstring> using nam ...

  6. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

  7. HDU 2896 病毒侵袭(AC自动机水)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. HDU 2896 病毒侵袭(AC自动机)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 2896 病毒侵袭【AC自动机】

    <题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...

  10. hdu2896 病毒侵袭 ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. docker端口映射或启动容器时报错Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen

    现象: [root@localhost ~]# docker run -d -p 9000:80 centos:httpd /bin/sh -c /usr/local/bin/start.shd5b2 ...

  2. WCF Rest用法

    GET GET单参数 服务器 [OperationContract] string GetOneParameter(string value); [WebInvoke(Method = "G ...

  3. C# System.Threading.Timer的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. Expression Blend学习5控件

    原文:Expression Blend学习5控件 Expression Blend ButtonStyle- TextButton 本章以TextButton为例,讲解如何最简单,最快速的制作一个专业 ...

  5. MinDoc v0.6 发布,轻量级文档在线管理系统

    更新日志 新增 标签功能,可以根据标签组织项目 新增 用户删除功能,删除后的用户项目以及其他数据会自动转移到超级管理员账户上 新增 项目描述支持Markdown语法 优化 项目标签添加效果 优化 登录 ...

  6. 零元学Expression Blend 4 - Chapter 24 以实作了解Cover Flow功能

    原文:零元学Expression Blend 4 - Chapter 24 以实作了解Cover Flow功能 今天要介绍一个Silverlight Toolkit内好用且在图片展示操作上很常见的元件 ...

  7. SOA 相关开发调试软件

    开发工具 IntelliJ IDEA:https://www.jetbrains.com/idea/ SOA调试 soapui:http://www.soapui.org/ wcfstorm:http ...

  8. getch(),getche(),getchar()的区别

    先说基本区别. (1) getch()和getche()函数     这两个函数都是从键盘上读入一个字符.其调用格式为:      getch();      getche();     两者的区别是 ...

  9. Elasticsearch ML

    Elastic公司在收购了Prelert半年之后,终于在Elasticsearch 5中推出了Machine Learning功能.Prelert本身就擅长做时序性数据的异常检测,从这点上讲也比较契合 ...

  10. CMake编译Widget UI Qt程序

    自从CMake被引入到KDE项目的编译系统中后,CMake的使用者日益增多,Qt也不例外,除了使用QMAKE编译Qt程序外,也可以使用CMake来编译Qt程序,并且CMake在使用上更灵活,特别是大型 ...