Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 79383    Accepted Submission(s): 27647

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
 
Author
Wiskey
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3065 2243 2825 3341 3247 
 

题意:

给定n个模式串和一个文本串,统计文本串中模式串的个数。

思路:

AC自动机模板。

多组数据一定要注意初始化!

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int t, n;
const int maxn = 5e5 + ;
const int maxlen = 1e6 + ; struct tree{
int fail;
int son[];
int ed;
}AC[maxlen];
int tot = ;
char s[maxlen]; void build(char s[])
{
int len = strlen(s);
int now = ;
for(int i = ; i < len; i++){
if(AC[now].son[s[i] - 'a'] == ){
AC[now].son[s[i] - 'a'] = ++tot;
}
now = AC[now].son[s[i] - 'a'];
}
AC[now].ed += ;
} void get_fail()
{
queue<int>que;
for(int i = ; i < ; i++){
if(AC[].son[i] != ){
AC[AC[].son[i]].fail = ;
que.push(AC[].son[i]);
}
}
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = ; i < ; i++){
if(AC[u].son[i] != ){
AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
que.push(AC[u].son[i]);
}
else{
AC[u].son[i] = AC[AC[u].fail].son[i];
}
}
}
} int AC_query(char s[])
{
int len = strlen(s);
int now = , ans = ;
for(int i = ; i < len; i++){
now = AC[now].son[s[i] - 'a'];
for(int t = now; t && AC[t].ed != -; t = AC[t].fail){
ans += AC[t].ed;
AC[t].ed = -;
}
}
return ans;
} int main()
{
scanf("%d", &t);
while(t--){
for(int i = ; i <= tot; i++){
AC[i].ed = ;
AC[i].fail = ;
for(int j = ; j < ; j++){
AC[i].son[j] = ;
}
}
tot = ;
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%s", s);
build(s);
}
AC[].fail = ;
get_fail();
scanf("%s", s);
printf("%d\n", AC_query(s));
}
return ;
}

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. flume 增量上传日志文件到HDFS中

    1.采集日志文件时一个很常见的现象 采集需求:比如业务系统使用log4j生成日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs中. 1.1.根据需求,首先定义一下3大要素: 采集源 ...

  2. linux alias使用

    安装一些软件 python redis mysql jdk,都需要添加环境变量,一个路径添加到了环境变量,就可以直接在linux控制台输入  xx命令. 否则不添加环境变量,使用xx就会提示找不到命令 ...

  3. php 自定义 分页函数

    <?php /** * 分页函数 * @param type $num 数据总数 * @param type $perpage 每页总数 * @param type $curpage 当前分页 ...

  4. RESTful状态码说明

    https://www.zhihu.com/question/58686782/answer/159603453 常用状态码: 200 请求成功并返回所需资源 400 客户端请求有语法错误 401 未 ...

  5. 32位win7+vs2008编译mysql 5.6.22源码并安装

    以下这部分安装说明是来自http://www.2cto.com/database/201407/316681.html的win7+vs2010源码编译mysql,文章最后会说明用vs2008编译遇见的 ...

  6. 【Java并发编程五】信号量

    一.概述 技术信号量用来控制能够同时访问某特定资源的活动的数量,或者同时执行某一给定操作的数据.计数信号量可以用来实现资源池或者给一个容器限定边界. 信号量维护了一个许可集,许可的初始量通过构造函数传 ...

  7. c# MVC Take的使用

    Take的使用 myPicture = dbContext.MyPictures.Where(u => u.Width == request.Width && u.Height ...

  8. HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP错误解决方法

    在树莓派上运行在windows上正确的程序,  报错: HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP OpenCV Error: Assertion failed (s ...

  9. Esper学习之十一:EPL语法(七)

    上一篇说到了EPL如何访问关系型数据库这种数据源,实际上别的数据源,比如:webservice.分布式缓存.非关系型数据库等等,Esper提供了统一的数据访问接口.然后今天会讲解如何创建另外一种事件类 ...

  10. python tkinter Listbox用法

    python tkinter组件的Listbox的用法,见下面代码的演示: from tkinter import * root=Tk() v=StringVar() #Listbox与变量绑定' l ...