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. bigdata_mac下安装spark_scala

    Java 下载安装Mac对应版本的JDK. Apache-spark $ brew update $ brew info apache-spark $ brew install apache-spar ...

  2. dotnet pack 打包文件版本号引起 "Could not load file or assembly" 问题

    如果不是遇到,真的不会想到,代码世界的问题真是千奇百怪,这次遇到的是 dotnet pack 打包文件版本号引起的问题. 之前进行 nuget 打包都是在 Visual Studio build 时进 ...

  3. 利用最小二乘法拟合任意次函数曲线(C#)

    原文:利用最小二乘法拟合任意次函数曲线(C#) ///<summary>     ///用最小二乘法拟合二元多次曲线     ///</summary>     ///< ...

  4. WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化)

    原文:WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化) 注释直接写在代码里了   不太理解意思的 可以先去看看我上一篇  WPF ScrollViewer(滚动条)  自定 ...

  5. WPF之VirtualizingStackPanel.IsVirtualizing="False"

    原文:WPF之VirtualizingStackPanel.IsVirtualizing="False" 相信从winform转到wpf的人都遇到过这样的困惑,在处理DataGri ...

  6. 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source

    原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlData ...

  7. Oracle VM VirtualBox ubuntu 共享文件设置

    1.创建共享文件 2.在本机上选择共享文件路径,虚拟机设置共享文件名称,注意这里不能选择自动挂载 3. 虚拟机新建文件夹挂载共享文件 sudo mkdir /mnt/sharedsudo mount ...

  8. socket上http协议应用(使用socket进行http通信的例子,准备好报头以后,简单read/write就可以了)

    前几天看socket本有点晕, 好不容易弄明白了,才发现公司服务器用的是http的. 找了好久也没发现linux下直接用http的api, 不过今日偶然发现了使用socket进行http通信的例子, ...

  9. Win8Metro(C#)数字图像处理--2.6图像对比度调整

    原文:Win8Metro(C#)数字图像处理--2.6图像对比度调整  2.6图像对比度调整函数 [函数名称] 图像对比度调整函数ContrastAdjustProcess(WriteableBi ...

  10. 用python & bat写软件安装脚本 + HM NIS Edit自动生成软件安装脚本

    2019-03-11更新:原来NSIS脚本也可以禁用64位文件操作重定向的! 1.在安装脚本的开始处定义 LIBRARY_X64. !include "MUI.nsh"!inclu ...