HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版。由于这里有多个text。
那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术。我直接使用个vis数组记录已经訪问过的节点,达到加速效果,速度还算挺快的。
只是看discuss里面有人直接使用Trie,做出了140ms的速度,并且他的程序严格来说并不对。可见本题的数据非常水啊。Trie的时间效率肯定比AC自己主动机低,可是在数据非常水的特殊情况下。Trie的速度也能够非常快的。
注意两个细节:
1 病毒也须要安装顺序输出,不小心就遗漏了。这里害我错了几次。
2 还有就是题目的字符不确定是多少。使用33-94范围的字符測试答案正确的。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
const int MAX_N = 501;
const int MAX_WORD = 201;
const int MAX_M = 1001;
const int MAX_TXT = 10001;
const int MAX_V = 3;
const int ARR_SIZE = 94;
char virus[MAX_WORD], web[MAX_TXT];
int webNum[MAX_V];
int M, N; inline int getIndex(char ch) { return ch - 33; } struct Node
{
int n, num, id;
Node *fail;
Node *arr[ARR_SIZE];
}; void clearNode(Node *rt)
{
rt->n = 0;
rt->num = 0;
rt->id = 0;
rt->fail = NULL;
for (int i = 0; i < ARR_SIZE; i++)
{
rt->arr[i] = NULL;
}
} Node gPool[MAX_N*MAX_WORD], *Trie;
int gPoolID;
bool vis[MAX_N*MAX_WORD]; //怎么老是忘记使用visited技术的? void insertTrie(char *virus, int num)
{
Node *pCrawl = Trie;
for (; *virus; virus++)
{
int id = getIndex(*virus);
if (!pCrawl->arr[id])
{
pCrawl->arr[id] = &gPool[gPoolID++];
clearNode(pCrawl->arr[id]);
pCrawl->arr[id]->id = gPoolID-1;
}
pCrawl = pCrawl->arr[id];
}
pCrawl->n++;
pCrawl->num = num;
} void buildFail()
{
queue<Node *> qu;
qu.push(Trie);
while (!qu.empty())
{
Node *p = qu.front(); qu.pop();
for (int i = 0; i < ARR_SIZE; i++)
{
if (!p->arr[i]) continue;
p->arr[i]->fail = Trie;
Node *fail = p->fail;
while (fail)
{
if (fail->arr[i])
{
p->arr[i]->fail = fail->arr[i];
break;
}
fail = fail->fail;
}
qu.push(p->arr[i]);
}
}
} int searchVirus(char *web)
{
int n = 0;
Node *pCrawl = Trie;
memset(vis, 0, sizeof(bool)*(gPoolID+1));
for (; *web; web++)
{
int i = getIndex(*web);
while (!pCrawl->arr[i] && pCrawl != Trie) pCrawl = pCrawl->fail;
if (pCrawl->arr[i])
{
pCrawl = pCrawl->arr[i];
Node *p = pCrawl;
while (p && !vis[p->id])//使用vis比直接检查数组值快捷方便
{
if (p->n) webNum[n++] = p->num;
vis[p->id] = true;
p = p->fail;
}
if (n == MAX_V) break; //At most MAX_V virus one web
}
}
return n;
} int main()
{
Trie = &gPool[0];
while (scanf("%d", &N) != EOF)
{
gPoolID = 1;
clearNode(Trie);
getchar();
for (int i = 1; i <= N; i++)
{
gets(virus);
insertTrie(virus, i);
}
buildFail(); scanf("%d", &M);
getchar();
int cnt = 0;
for (int i = 1; i <= M; i++)
{
gets(web);
int n = searchVirus(web);
if (n)
{
cnt++;
printf("web %d:", i); sort(webNum, webNum + n);//Watch out: order!
for (int j = 0; j < n; j++)
{
printf(" %d", webNum[j]);
}
putchar('\n');
}
}
printf("total: %d\n", cnt);
}
return 0;
}
HDU 2896 病毒侵袭 AC自己主动机题解的更多相关文章
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2896 病毒侵袭 AC自动机 基础题
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )
BFS... -------------------------------------------------------------------------------------------- ...
- Oracle 11g New 热补丁
热补丁:概览 对于Oracle 实例上的bug 修复或诊断补丁程序,热补丁 可以执行以下操作: • 安装 • 启用 • 禁用 热补丁:概览 使用热补丁可以安装.启用和禁用 正在运行的 活动Oracle ...
- [LeetCode]题解(python):029-Divide Two Integers
题目来源: https://leetcode.com/problems/divide-two-integers/ 题意分析: 不用乘法,除法和mod运算来实现一个除法.如果数值超过了int类型那么返回 ...
- UnixShell编程(第三版) 二章
这本书写的真的很好,让人欲罢不能的读下去. 1,可以简单的将命令看做类似函数的东西,而后面跟的是所传的参数. echo Hello Word > Hi 将字符串hello Wrod 写入文件 ...
- 转:requirejs打包压缩r.js使用示例
为了应对日益复杂,大规模的JavaScript开发.我们化整为零,化繁为简.将复杂的逻辑划分一个个小单元,各个击破.这时一个项目可能会有几十个甚至上百个JS文件,每个文件为一个模块单元.如果上线时都是 ...
- USB接口的SmartCard Class协议标准:ICCD and CCID
ICCD是 Intergrated Circuit(s) card Device 的缩写.CCID是 Integrated Circuit(s) cards interface devices的缩写I ...
- Delphi 编写系统服务(服务控制线程:开始,暂停,继续,停止)
打开Delphi 7,新建,选择Other->Service Application 修改属性中Name和DisplayName,Name是Service的名称,决定着进程的标识,Display ...
- C语言中操作符的优先级大全
C语言中操作符的优先级大全, 当然c++, Objective-C,大部分语言都试用. 下面是来自The C Programming Language 2th的总结. OperatorsAssocia ...
- 2013 ACM/ICPC 成都网络赛解题报告
第三题:HDU 4730 We Love MOE Girls 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4730 水题~~~ #include < ...
- 【Maven】pom.xml 配置 eclipse错误
<!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactI ...