hdu----(3065)病毒侵袭持续中(AC自动机)
病毒侵袭持续中
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6721 Accepted Submission(s): 2372
t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多
好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的
战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
AA
BB
CC
ooxxCC%dAAAoen....END
CC: 1
Hit:
题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。
/*hdu 3065 Ac-自动机*/
//#define LOCAL
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int res;
struct Trie
{
struct Trie *fail;
struct Trie *child[];
int id;
};
char s1[][];
char t1[];
int ans[];
void _insert(char *s,Trie *root,int id) //构造一下rie树
{
Trie *cur=root,*newcur;
for(int i=;s[i];i++)
{
int idx=s[i]-;
if(cur->child[idx]==NULL)
{
newcur=new Trie;
for(int j=;j<;j++)
newcur->child[j]=NULL;
newcur->fail=NULL;
newcur->id=;
cur->child[idx]=newcur;
}
cur=cur->child[idx];
}
cur->id=id;
}
//构造失败指针
void ac_fail(Trie *root)
{
Trie *cur,*q ;
queue<Trie*>tre;
tre.push(root);
while(!tre.empty())
{
cur=tre.front();
tre.pop();
for(int i=;i<;i++)
{
if(cur->child[i])
{
if(cur==root)
cur->child[i]->fail=root;
else
{
q=cur;
while(q->fail){
if(q->fail->child[i])
{
cur->child[i]->fail=q->fail->child[i];
break;
}
q=q->fail;
}
if(!q->fail)
cur->child[i]->fail=root;
}
tre.push(cur->child[i]);
}
}
}
}
//查询
void solve(char *s,Trie *root)
{
Trie *cur=root,*newcur;
for(int i= ; s[i] ;i++ )
{
while(cur->child[s[i]-]==NULL&&cur!=root)
cur=cur->fail;
cur=cur->child[s[i]-];
if(cur==NULL) cur=root;
newcur=cur;
while(newcur!=root&&newcur->id>)
{
ans[newcur->id]++;
newcur=newcur->fail;
}
}
}
void del(Trie *root)
{
if(!root) return ;
for(int i=;i<;i++)
if(root->child[i])
del(root->child[i]);
delete root;
}
void work(int n)
{
for(int i=;i<=n;i++)
if(ans[i]>)
{
printf("%s: %d\n",s1[i],ans[i]);
}
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,i;
while(scanf("%d",&n)!=EOF)
{
Trie *root=new Trie;
for(i=;i<;i++)
root->child[i]=NULL;
root->fail=NULL;
root->id=;
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
scanf("%s",s1[i]);
_insert(s1[i],root,i);
}
ac_fail(root);
scanf("%s",t1);
solve(t1,root);
work(n);
del(root);
}
return ;
}
hdu----(3065)病毒侵袭持续中(AC自动机)的更多相关文章
- HDU 3065 病毒侵袭持续中 (AC自动机)
题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
- hdoj 3065 病毒侵袭持续中(AC自动机)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...
- HDU 3065 病毒侵袭持续中
HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3065 病毒侵袭持续中【AC自动机】
<题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...
- HDU 3065 病毒侵袭持续中(AC自己主动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3065 Problem Description 小t非常感谢大家帮忙攻克了他的上一个问题.然而病毒侵袭 ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu 3065病毒侵袭持续中
病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 3065 病毒侵袭持续中 (模板题)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU3065 病毒侵袭持续中 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- vi及缩进设置
set autoindent,把当前行的对起格式应用到下一行: set smartindent,智能的选择对起方式: set tabstop=4,设置tab键为4个空格: set shiftwidth ...
- MySQL(五) —— 子查询
子查询(SubQuery)是指出现在其他SQL语句内的SELECT语句. 如: SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2); 其中 SELE ...
- Hearthstone-Deck-Tracker项目的编译
https://github.com/HearthSim/Hearthstone-Deck-Tracker https://github.com/HearthSim/HearthDb https:// ...
- CSS布局口诀 - CSS BUG顺口溜
在进行CSS网页布局时遇到BUG,请认真阅读以下内容,非常容易记忆的,不知道哪位高人把CSS BUG编成了顺口溜了!看看好不好记住呢? 一.IE边框若显若无,须注意,定是高度设置已忘记: 二.浮动产生 ...
- [SAP ABAP开发技术总结]Form(subroutine)、Function参数传值传址
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- python_way day12 sqlalchemy,原生mysql命令
python_way day12 sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...
- XAF学习笔记之 Upcasting
通常,我们会定义继承层次结构,假设有类型,CustomerBase,CustomerTrialed,CustomerRegistered三个类型,并且继承结构如下: 业务对象代码定义如下: using ...
- PHP爬虫抓取网页内容 (simple_html_dom.php)
使用simple_html_dom.php,下载|文档 因为抓取的只是一个网页,所以比较简单,整个网站的下次再研究,可能用Python来做爬虫会好些. <meta http-equiv=&quo ...
- 【转】分析Linux和windows动态库
原文地址:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Lin ...
- js call与apply的区别-Tom
.apply和.call方法是在函数原型中定义的两个方法(因此所有的函数都可以访问它)允许去手动设置函数调用的this值,他们用接受 的第一个参数作为this值,this 在调用的作用域中使用.这两个 ...