[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 ...
随机推荐
- 学习Android之SharedPreferences使用
效果图例如以下: 当我们想让自己的属性设置保存下来,这时就须要SharedPreferences. 上面这个小程序,音乐状态是保存下来的.使用的上一次退出的状态. 进入DDMS,data文件下的dat ...
- [转]轻量级 Java Web 框架架构设计
工作闲暇之余,我想设计并开发一款轻量级 Java Web 框架,看看能否取代目前最为流行的而又越来越重的 Spring.Hibernate 等框架.请原谅在下的大胆行为与不自量力,本人不是为了重造轮子 ...
- 设计一个算法,求非空二叉树中指定的第k层(k>1)的叶子节点的个数
思想:採用基于层序遍历的方法. 用level扫描各层节点,若某一层的节点出队后.rear指向该层中最右节点.则将rear赋值给last(对于第一层.last=1).在出队时,若front=last,表 ...
- 推荐几本不错的ASP.NET MVC书
以前主要是做PHP应用的,由于工作需要,捡起来.NET, 特别是新技术层出不穷,找了几本书看,个人感觉还不错,网上也有电子版的下载 一. ASP.NET MVC4 Web 编程 O'Reilly出版社 ...
- android——混淆打包
网上搜了一大堆,在此不一一赘诉. 直接讲解了 如上图这么配置,其实就是加上一句话而已.告诉打包工具混淆打包的代码放在./proguard-project.txt这里 proguard.config=. ...
- CSS - 关于li中图文混排不能垂直居中的问题
图片和文字一起放在li标签下不能同时垂直居中 解决办法: 1.设置图片的position:absolute; 2.把文字加上span标签: span{ height:30px;line-heigh ...
- hdu1020Encoding
Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...
- java集合使用——HashMap
在map中插入.删除和定位元素时,HashMap是最好的选择.如果要按照自然顺序或自定义顺序遍历(获取所有元素),那么treemap更好一些. 第一:构造和添加元素 HashMap map = new ...
- nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)
使用windows版本的nginx启动时遇到(1113: No mapping for the Unicode character exists in the target multi-byte co ...
- BlockingQueue
BlockingQueue的使用 http://www.cnblogs.com/liuling/p/2013-8-20-01.html BlockingQueue深入分析 http://blog.cs ...