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自动机】的更多相关文章

  1. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  2. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  4. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  5. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  6. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  7. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  8. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  9. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. word2vector 使用方法 计算语义相似度

    参考:http://techblog.youdao.com/?p=915#LinkTarget_699word2vector是一个把词转换成词向量的一个程序,能够把词映射到K维向量空间,甚至词与词之间 ...

  2. Python基础笔记系列十三:socket网络编程

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!!使用python编写一个简易的服务端程序和客户端程序,启动服务端和客户端(监 ...

  3. HTML之实现页面缓存

    一般来说,对于html页面,一个站点,每个页面都会有相同的公共文件,比如页面的头部.尾部.侧边栏目.公共JS等.访问站点下的每一个页面,相同的公共文件,都需要重复从服务器下载.从性能和带宽角度看,重复 ...

  4. (转)SQL一次性插入大量数据

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  5. 以普通用户启动的Vim如何保存需要root权限的文件

    在Linux上工作的朋友很可能遇到过这样一种情况,当你用Vim编辑完一个文件时,运行:wq保存退出,突然蹦出一个错误: E45: 'readonly' option is set (add ! to ...

  6. 【BZOJ4819】新生舞会(分数规划,网络流)

    [BZOJ4819]新生舞会(分数规划,网络流) 题面 BZOJ Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买 ...

  7. mongodb安装与权限配置

    mongodb下载地址:官方下载IDE工具:Robo 3T:官方下载 windows系统要求64位,最低2g内存,推荐8g内存及以上 安装过程没有需要配置的地方,直接下一步到结束,如果想要方便可以在环 ...

  8. 数论基础算法总结(python版)

    /* Author: wsnpyo Update Date: 2014-11-16 Algorithm: 快速幂/Fermat, Solovay_Stassen, Miller-Rabin素性检验/E ...

  9. Netty实例几则

    Netty是基于JDK NIO的网络框架 简化了NIO编程, 不用程序自己维护selector, 将网络通信和数据处理的部分做了分离 多用于做底层的数据通信, 心跳检测(keepalived) 1. ...

  10. Prism 4 文档 ---第5章 实现MVVM模式

        MVVM模式有助于清楚的区分应用程序界面的业务层和展现层.保持一个清晰的应用程序逻辑和UI分离有助于处理开发和设计过程中大量的问题,同时,使得应用程序的测试,维护,和扩展更加容易.MVVM也可 ...