HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
AC自动机模板,加一个小优化,在处理失配指针的时候预处理节点经过失配后到达的节点
#include<bits/stdc++.h>
using namespace std;
#define N 1000010
struct Node{
int fail,cnt,ch[26];
void clean(){
fail=cnt=0;
memset(ch,0,sizeof(ch));
}
}t[N];
int tot,n,ans;
bool vis[N];
char s[N];
void init(){
tot=1;ans=0;
memset(vis,0,sizeof(vis));
t[0].clean();t[1].clean();
for(int i=0;i<26;i++)t[0].ch[i]=1;
}
int index(char c){return c-'a';}
void insert(char *s){
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int tmp=index(s[i]);
if(!t[u].ch[tmp])
t[t[u].ch[tmp]=++tot].clean();
u=t[u].ch[tmp];
}
t[u].cnt++;
}
void buildFail(){
static queue<int> q;
q.push(1);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<26;i++){
int w=t[u].ch[i],v=t[u].fail;
while(!t[v].ch[i])v=t[v].fail;
v=t[v].ch[i];
if(w){
t[w].fail=v;
q.push(w);
}else t[u].ch[i]=v;//失配预处理
}
}
}
void collect(int u){
while(u&&!vis[u]){
vis[u]=1;
ans+=t[u].cnt;
u=t[u].fail;
}
}
int main(){
int T;scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
insert(s);
}
buildFail();
scanf("%s",s);
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int tmp=index(s[i]);
u=t[u].ch[tmp];
collect(u);
}
printf("%d\n",ans);
}
return 0;
}
HDU2222 Keywords Search 【AC自动机】的更多相关文章
- 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 ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
随机推荐
- 使用xunit对asp.net core webapi进行集成测试
新项目我们采用前后端分离,后端采用asp.net core webapi, 如何对后端代码进行自动化测试呢,有以下几种方案: 1. 单元测试,目前这个方案对我们来说难度很大,抛开时间的问题,单元测试对 ...
- Entity Framework 基于Oracle的code first 问题汇总
1. 在code first 在数据库中建表时,需要指定schema, 默认是dbo, 需要改成我们的oracle登录名 protected override void OnModelCreating ...
- Hive 导入、导出数据
<------ 导入 ------> 从本地目录文件导入Hive表 语法:LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABL ...
- python 读取、保存、二值化、灰度化图片+opencv处理图片的方法
http://blog.csdn.net/johinieli/article/details/69389980
- Spoj-COINS-记忆化dp
COINS - Bytelandian gold coins #dynamic-programming In Byteland they have a very strange monetary sy ...
- 【Raspberry Pi】USB无线网卡自动连接
Raspberry Pi 使用USB无线网卡的时候不会因为路由重启而掉线. #!/bin/bash while true ; do if ifconfig wlan0 | grep -q " ...
- iptables详解(8):iptables扩展模块之state扩展
当我们通过http的url访问某个网站的网页时,客户端向服务端的80端口发起请求,服务端再通过80端口响应我们的请求,于是,作为客户端,我们似乎应该理所应当的放行80端口,以便服务端回应我们的报文可以 ...
- influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB
influxdb influxdb是最新的一个时间序列数据库,最新一两年才产生,但已经拥有极高的人气.influxdb 是用Go写的,0.9版本的influxdb对于之前会有很大的改变,后端存储有Le ...
- firefox与ie 的javascript区别
1. Document.form.item 问题 现有问题: 现有代码中存在许多 document.formName.item("itemName") 这样的语句,不能在 ...
- LeetCode OJ:Maximal Rectangle(最大矩形)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...