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. python内存泄漏,python垃圾手动回收,1

    部署的舆情系统,内存变大,找原因. 一个小例子. def func(): local_list = list(range(10000000)) func() time.sleep(200) 能够观察到 ...

  2. SQL Server 2008 清空删除日志文件 130G日志 10秒内变10M

    SQL2005: Backup Log DNName with no_log  '这里的DNName是你要收缩的数据库名,自己注意修改下面的数据库名,我就不再注释了. go dump transact ...

  3. IOS UILineBreakMode的各种情况分析

    typedef enum {    UILineBreakModeWordWrap = 0,    UILineBreakModeCharacterWrap,    UILineBreakModeCl ...

  4. SQL利用CASE按分组显示合计

    按行显示的合计 select game, sum(purchase) as purchase_sum from purchase group by game; 按列显示的合计 select sum(c ...

  5. 【Android】水平居中 垂直居中 中心居中

    android:layout_centerInParent 将该组件放置于水平方向中央及垂直中央的位置 android:layout_centerHorizontal 将该组件放置于水平方向中央的位置 ...

  6. 【安全开发】C/C++安全编码规范

    C本质上是不安全的编程语言.例如如果不谨慎使用的话,其大多数标准的字符串库函数有可能被用来进行缓冲区攻击或者格式字符串攻击.但是,由于其灵活性.快速和相对容易掌握,它是一个广泛使用的编程语言.下面是针 ...

  7. java serialize 浅谈

    对象的串行化(Serialization) 一.串行化的概念和目的 1.什么是串行化 对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把 ...

  8. Android中的安全与访问权限控制

    Android是一个多进程系统,在这个系统中,应用程序(或者系统的部分)会在自己的进程中运行.系统和应用之间的安全性是通过Linux的facilities(工具,功能)在进程级别来强制实现的,比如会给 ...

  9. g++参数介绍

    转自http://www.cnblogs.com/lidan/archive/2011/05/25/2239517.html [介绍] gcc and g++分别是gnu的c & c++编译器 ...

  10. 转:桩模块 stub 和驱动模块 driver

    迷惑我很久的stub的概念,今天终于看到觉得靠谱的了,原文地址:http://xyzhaoangela.blog.hexun.com/14208786_d.html 桩模块stub:集成测试前要为被测 ...