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 ...
随机推荐
- VUE --- 给页面加点网络动态数据
这时候的页面都是静态的(数据在写程序的时候已经固定了不能修改),而每个应用基本上都会请求外部数据以动态改变页面内容.对应有一个库叫 vue-resource 帮我们解决这个问题. 使用命令行安装 cn ...
- Sqlite创建增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- ionic蓝牙用法
插件cordova官网cordova-plugin-ble-central 1.开启本机蓝牙 ble.enable( function() { console.log("Bluetooth ...
- 【转】Java面试题全集2.2(下)
154.如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <context-par ...
- Thinking in java学习笔记之多态
多态是一种将改变的事物和未变的事物分离开来的重要技术.
- Hadoop中pid文件存储
我的hadoop集群部署在自己电脑虚拟机上,有时候我是挂起虚拟机,第二天再打开发现有些线程就挂了,比如namenode,好奇怪,当时看了一些帖子说是和pid存储有关,找到log看到找不到pid.因为基 ...
- arcgis engine 监听element的添加、更新和删除事件(使用IGraphicsContainerEvents)
IGraphicsContainerEvents Interface 如何监听 element事件? 如,当我们在Mapcontrol上添加.删除.更新了一个Element后,如何捕捉到这个事件? ...
- Canvas实例
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- coredump简介与coredump原因总结
from:http://www.cnblogs.com/doctorqbw/archive/2011/12/21/2295962.html 千兵卫博士 coredump简介与coredump原 ...
- 今天在学习NTP时发现了2个网站
NTP 调整系统时间 一个网站是:http://chrony.tuxfamily.org/doc/1.31/manual.html 这个是专门介绍chrony的,做的很详细. 另外一个是:http: ...