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 ...
随机推荐
- JAVA初学者(一)
2015-12-15 21:26:17 刚学的java 做个总结: 1.构造函数没有返回值. 2.A对象调用Q的方法,Q方法里的变量就是A的变量 Fraction add(Fraction f) 在 ...
- 实现Runnable和继承Thread的区别
啥都先不说,运行两段程序看看结果再分析 实现Runnable接口的程序代码 public class ThreadTest1 implements Runnable { private int num ...
- 分布式系统理论:一致性协议Paxos
Paxos算法是莱斯利·兰伯特(Leslie Lamport)于1990年提出的一种基于消息传递的一致性算法. Paxos 算法是一个解决分布式系统中,多个节点之间就某个值(注意是某一个值,不是一系列 ...
- 推荐给开发者的11个PHP框架(转)
PHP框架对于Web开发者来说是非常有用的工具,它可以帮助使用者更快.更容易的完成项目.根据调查,PHP仍是Web开发中最受欢迎和最实用的平台之一.当谈及Web开发时,很多人依然会选择使用PHP框架, ...
- thinkphp getField("xxxxx", true); 得到一个字段所有值组成的的数组
很多时候我们只需要一张表里某个字段的值,组成的数组 $Channel = D('channel');$channelList = $Channel->order('user_name')-> ...
- php redis 秒杀demo
$redis = new Redis(); $redis->connect("127.0.0.1", "6379"); $redis->select ...
- 在Web API 2 中实现带JSON的Patch请求
译文:http://www.cnblogs.com/kexxxfeng/p/the-patch-verb-in-web-api-2-with-json.html 原文:https://carly.io ...
- C++(二十五) — 类的封装、实现
1.类的封装.实现.对象的定义及使用 (1)类是一组对象的抽象化模型.类对象将数据及函数操作集合在一个实体中,只需要接口,而不需要知道具体的操作. 隐藏细节,模型化: 类内自由修改: 减少耦合,相当于 ...
- C#/JAVA 程序员转GO/GOLANG程序员笔记大全(DAY 04)
-------------------- interface 接口 // 定义: type IHumaner interface { SayHi() // 接口中只能是方法声明,没有实现,没有数据字段 ...
- 用Qt写了一个qq客户端,采用webqq协议,发出来和大家分享一下---大神请无视
首先做以下声明: 本程序基于腾讯公司的webqq协议开发,所有相关版权归腾讯公司所有.此程序只用于技术交流和学习,不得用于其他方面. ---开发者:雨后星辰,转载请注明出处:http://www.cn ...