[AC自动机][HDU3065]
//======================
// HDU 2222
// 求目标串中出现了几个模式串
//输入
//1
//5
//she
//he
//say
//shr
//her
//yasherhs
//====================
//这道题如果for一遍把不属于A-Z的变量改成Z+1 反而会超时 直接用next[1000*55][128]搞定算了
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
char S[1001][51];
int num[1001];
using namespace std;
struct Trie
{
int next[1000*55][128],fail[1000*55],end[1000*55];
int root,L;
int newnode()
{
for(int i=0;i<128;i++)
next[L][i]=-1;
end[L++]=0;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void insert(char buf[],int p)
{
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]=p;
}
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; //第一层的失败指针指向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 len=strlen(buf);
int now=root;
int res=0;
for(int i=0;i<len;i++)
{
now=next[now][buf[i]];
int temp=now;
while(temp!=root)
{
num[end[temp]]++;
temp=fail[temp];
}
}
return res;
}
void debug()
{
for(int i=0;i<L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = 0;j <= 26;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
};
int n;
char s[300];
char buff[2000005];
Trie ac;
void input()
{
memset(S,0,sizeof(S));
memset(num,0,sizeof(num));
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%s",S[i]);
ac.insert(S[i],i);
}
ac.build();
}
int main()
{
// freopen("a.in","r",stdin);
int T;
while(cin>>n)
{
input();
scanf("%s",buff);
ac.query(buff);
for(int i=1;i<=n;i++)
if(num[i]>0)
{
printf("%s: %d\n",S[i],num[i]);
}
}
return 0;
}
[AC自动机][HDU3065]的更多相关文章
- 【HDU3065】 病毒侵袭持续中(AC自动机)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- [hdu3065]病毒侵袭持续中(AC自动机)
题意:给出多种病毒的号码和特征码,计算在某串中各病毒匹配的次数. 解题关键:AC自动机模板题,多组输入坑人. #include<bits/stdc++.h> using namespace ...
- [HDU3065]病毒持续侵袭中(AC自动机)
传送门 AC自动机的又一模板,统计每个字符串在文本中的次数. 所以就不需要vis数组了. ——代码 #include <cstdio> #include <cstring> # ...
- AC自动机小结
专题链接 第一题--hdu2222 Keywords Search ac自动机的模板题,入门题. 题解 第二题--hdu2896 病毒侵袭 一类病毒的入门题,类似模板 题解 第三题--hdu3 ...
- AC自动机基础知识讲解
AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...
- AC自动机总结
AC自动机的模板 void buildAC() { while(!q.empty()) q.pop(); q.push(); while(!q.empty()) { int x=q.front();q ...
- AC自动机专题总结
最近学习了AC自动机,做了notonlysuccess大牛里面的题,也该来个总结了. AC自动机(Aho-Corasick Automaton)在1975年产生于贝尔实验室,是著名的多模匹配算法之一. ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
随机推荐
- gcc基本用法
GCC基本用法 GCC最基本的用法是: gcc [option] filenames option:编译器所需要的编译选项 filenames:要编译的文件名 gcc编译流程 都以 hello.c 为 ...
- BFC,IFC,GFC,FFC的定义及功能
What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定 ...
- box-shadow讲解1
谈谈box-shadow的具体使用方法 语法: E {box-shadow: <length> <length> <length>?<length>?| ...
- Cisco cmd 命令
1.enable 开启全局配置模式:disable 禁用配置模式 2.config进入配置模式 3.line 设置进入用户模式密码:分为 line aux 0;line console 0;line ...
- UIScrollView 代理方法
在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...
- Python3.5入门学习记录-条件控制
Python的条件控制同C#一样,都是通过一条或多条语句的执行结果(True OR False)来决定执行的代码块. if 语句 Python中if语句的一般形式如下所示: if condition_ ...
- js 原型
1: function Person (name,age) { 2: this.name = name; 3: this.age = age; 4: } 5: 6: Person.prototyp ...
- JQuery中阻止事件冒泡的两种方式及其区别
JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...
- [ZooKeeper研究]二 ZooKeeper协议介绍
前面介绍了ZooKeeper的基本知识,这一节我们介绍一下ZooKeeper使用的协议.只有了解了ZooKeeper的协议,才能更好得理解ZooKeeper源代码的实现.ZooKeeper使用的是Za ...
- 阿里巴巴JAVA常考面试题及汇总答案
一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的? 答: 1.String是字符串常量,StringBuffer和StringB ...