Keywords Search(hdu 2222)
题意:给出n个单词,一篇文章,询问有几个单词在文章中出现过。
/*
AC自动机的裸题。
题目标号牛的一比。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 500010
#define M 1000010
using namespace std;
int a[N][],danger[N],point[N],mark[N],q[N],n,size;
char ch[M];
void insert(){
int len=strlen(ch),now=;
for(int i=;i<len;i++){
int t=ch[i]-'a';
if(!a[now][t])a[now][t]=++size;
now=a[now][t];
}
danger[now]++;
}
void acmach(){//构建自动机
int h=,t=;
q[]=;point[]=;
while(h<t){
int now=q[++h];
for(int i=;i<=;i++){
if(!a[now][i])continue;
int k=point[now];
while(!a[k][i])k=point[k];
point[a[now][i]]=a[k][i];//类似于kmp的失败数组
q[++t]=a[now][i];
}
}
}
void solve(){
int len=strlen(ch),k=,ans=;
for(int i=;i<len;i++){
mark[k]=;
int t=ch[i]-'a';
while(!a[k][t])k=point[k];
k=a[k][t];
if(mark[k])continue;
for(int j=k;j;j=point[j]){
ans+=danger[j];
danger[j]=;//每个单词只记录一次
}
}
printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);
while(T--){
memset(a,,sizeof(a));
memset(point,,sizeof(point));
memset(danger,,sizeof(danger));
memset(mark,,sizeof(mark));
size=;
for(int i=;i<=;i++)a[][i]=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",ch);
insert();
}
acmach();
scanf("%s",ch);
solve();
}
return ;
}
Keywords Search(hdu 2222)的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
- hdu----(2222)Keywords Search(ac自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
- HDU 2222 Keywords Search(AC自动机)题解
题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...
- hdu 2222 Keywords Search(AC自动机)
/* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...
- 【HDU】2222 Keywords Search(AC自动机)
题目 传送门:QWQ 分析 $ AC $自动机模板,黈力的码风真的棒极了,这是我抄他的. 还有 题号不错 代码 #include <cstdio> #include <cstring ...
随机推荐
- python文件读写操作
操作文件时一般经历以下步骤: 打开文件 操作文件 1.打开文件: 文件句柄 = open('/path/to/file_name','pattern') pattern: r:只读模式,打开文件时默认 ...
- IE下a标签后面的span元素向右浮动后错位
错误原因span放在了a标签之后 正确写法是放在之前 如下: <li><span>2016-07-29</span><a href="#" ...
- C#-和时间有关的计算代码、时间相减 得到天数、小时、分钟、秒差
asp.net(C#)时间相减 得到天数.小时.分钟.秒差 asp.net(C#)时间相减 得到天数.小时.分钟.秒差 DateTime dtone = Convert.ToDateTime( ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- html5图片异步上传/ 表单提交相关
1 form 表单 get/post提交时候. action地址(或者啥ajax的url地址) 会涉及到跨域问题 常见跨域问题http://www.cnblogs.com/rainman/archiv ...
- myeclipse给项目改了名字,但部署tomcat的项目名还是原来的
如标题所示: 在myeclipse中按F2改了项目名称,之前在tomcat中部署的名称是另一个,再次重新部署,永远是上一个的旧名称 解决办法: 项目点右键-properties-左上角搜索框输入web ...
- 1 云计算系列之云计算概述和KVM虚拟化简介
为什么会出现云之传统数据中学面临的问题 物理服务器的利用率非常低,浪费资源,且资源分配不合理,比如一台服务器CPU使用率不到40%,或者某个应用需要的硬件配置低但是服务器硬件配置高等等. 云计算概念 ...
- 创建面注记PolygonElement
1.根据4点创建一个面 /// <summary> /// 根据4个点创建图形,点序要顺时针 /// </summary> /// <param name="p ...
- GDB调试D语言
GDB7.2后开始支持对D语言的调试 GUI前端 http://beej.us/guide/bggdb/#compiling GDB教程 http://blog.csdn.net/haoel/arti ...
- AngularJS常用插件与指令收集
angularjs 组件列表 bindonce UI-Router Angular Tree angular-ngSanitize模块-$sanitize服务详解 使用 AngularJS 开发一个大 ...