HDU2222:Keywords Search——题解
http://acm.hdu.edu.cn/showproblem.php?pid=2222
题目大意:
给定 n 个长度不超过 50 的由小写英文字母组成的单词,以及一篇长为 m 的文章,问有多少个单词在文章中出现了,多组数据。
——————————————————————
AC自动机裸题。
(C++AC,G++TLE我能说什么……)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int N=1e4+;
const int M=1e6+;
const int L=+;
struct trie{
int ed;
int a[];
int fail;
void clear(){
ed=fail=;
memset(a,,sizeof(a));
return;
}
}tree[N*L]={};
char s[M];
int cnt=;
inline void insert(){
int now=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
if(!tree[now].a[t]){
cnt++;
tree[now].a[t]=cnt;
tree[cnt].clear();
}
now=tree[now].a[t];
}
tree[now].ed++;
return;
}
int q[N*L];
void getfail(){
int r=;
//以下是对第一层的特殊处理
for(int i=;i<;i++){
if(tree[].a[i]!=){
tree[tree[].a[i]].fail=;
r++;q[r]=tree[].a[i];
}
}
for(int l=;l<=r;l++){
int u=q[l];
for(int i=;i<;i++){
if(tree[u].a[i]!=){
tree[tree[u].a[i]].fail=tree[tree[u].fail].a[i];
r++;q[r]=tree[u].a[i];
}else{
tree[u].a[i]=tree[tree[u].fail].a[i];
}
}
}
return;
}
int check(){
int now=,ans=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
now=tree[now].a[t];
for(int j=now;j&&tree[j].ed!=-;j=tree[j].fail){
ans+=tree[j].ed;
tree[j].ed=-;
}
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
tree[].clear();
cnt=;
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>s;
insert();
}
getfail();
cin>>s;
cout<<check()<<endl;
}
return ;
}
HDU2222:Keywords Search——题解的更多相关文章
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- JSOI2012 玄武密码 和 HDU2222 Keywords Search
玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu----(2222)Keywords Search(trie树)
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 ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 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 ...
随机推荐
- Visual Studio 起始页中不显示最近使用的项目的解决办法
将 HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/NoRecentDocsHistory的 ...
- java对象创建过程简介
这是看书的记录,字有点丑啊还是将就搬上来 -.-,等把后面看了完善图
- 第七模块:项目实战一 第1章 项目实战:CRM客户关系管理系统开发
01-crm介绍 02-权限系统介绍 03-第一版表结构设计 04-第二版表结构设计 05-orm中创建表结构 06-销售管理系统业务 07-销售管理系统权限信息录入 08-快速实现简单的权限控制的设 ...
- 初学Direct X(9) ——文字的显示
初学Direct X(9) --文字的显示 本次学习如何使用ID3DXFont创建字体,使得我们可以在任何安装了Windows系统中TrueType字体来打印文字,不过最好使用标准字体,这样文字在每一 ...
- Siki_Unity_1-4_C#编程(零基础)
1-4 C#编程(零基础) 任务1:第一章课程资料 任务2:简介 任务3:安装设置IDE工具 Unity内置IDE:MonoDevelop 推荐Visual Studio 下载/安装 VS Commu ...
- 微信小程序之注释出现的问题(.json不能注释)
js的注释一般是双斜杠// 或者是/**/这样的快注释 .json是配置文件,其内容必须符合json格式内部不允许有注释. JSON有两种数据结构: 名称/值对的集合:key : value样式: 值 ...
- Django学习总结- ③
对象属性与继承关系: 对象属性 1. 显示属性 - 开发者手动定义的,直接看的到的 2. 隐式属性 - 系统根据需求,自动创建的对象 - objects 它是model.Manager对象 - 当我们 ...
- Stunnel客户端安装和配置
声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(DCS)的用户指南. 本文以公网访问华为云分布式缓存服务的Redis缓存为示例,介绍Stunnel客户端在各操作系统下的安装和配置. Stunn ...
- 从零开始的Python学习Episode 4——列表
一.列表 列表与数组相似,定义一个列表 a=[1,2,3,4,5] 1.基本操作 a=[1,2,3,4] #切片 范围取值时,包括第一项但不包括最后一项,顾头不顾尾 print(a[0:]) #从头到 ...
- 基于Hadoop2.5.0的集群搭建
http://download.csdn.net/download/yameing/8011891 一. 规划 1. 准备安装包 JDK:http://download.oracle.com/otn ...