• Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total
Submission(s):
39064    Accepted
Submission(s): 12596
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

题意:看现在的主字符串,有多少个零碎的字符串构成,
做法:AC自动机模板
代码如下:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#define maxn 500010
using namespace std;
struct Tire
{
int next[maxn][],fail[maxn],end[maxn];
int root,L;
int newnode()
{
for(int i=; i<; i++)
{
next[L][i] = -;
}
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i=;i<len;i++)
{
if(next[now][buf[i] - 'a'] == -)
{
next[now][buf[i] -'a'] = newnode();
}
now = next[now][buf[i] -'a'];
}
end[now]++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i=;i<;i++)
{
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int query(char buf[])
{
int len = strlen(buf);
int now = root;
int res = ;
for(int i=;i<len;i++)
{
now = next[now][buf[i] -'a'];
int tmp = now;
while(tmp != root)
{
res += end[tmp];
end[tmp] = ;
tmp = fail[tmp];
}
}
return res;
}
};
char buf[];
Tire ac;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ac.init();
for(int i=;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}

HDU-2222的更多相关文章

  1. HDU 2222  AC自动机模板题

    1.HDU 2222 2.题意:给出n个单词,一个字串,求有多少个单词在字串里出现了.注意给出的单词可能会重复,重复的不计. 3.总结:入门题.在查询这里还是不太懂. #include<bits ...

  2. HDU 2222 Keywords Search(AC自动机模版题)

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

  3. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  4. HDU 2222 最简单的AC自动机套模板应用

    HDU 2222 题意:给出N(N<=10,000)个单词,每个单词长度不超过50.再给出一个字符串S,字符串长度不超过1,000,000.问有多少个单词出现在了字符串S中.(单词可能重复,单词 ...

  5. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  6. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  7. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

  8. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  9. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  10. HDU 2222:Keywords Search(AC自动机模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

随机推荐

  1. 常州day1p4

    给定一棵 n 个点的树,树上每个节点代表一个小写字母,询问一个字符串 S 是否在树上出现过? 字符串 S 出现过即表示存在两个点 u,v,u 到 v 的最短路径上依次连接所有点上的字母恰好是 S N ...

  2. ZOJ3899 State Reversing 【线段树 + NTT】

    题目链接 ZOJ3899 题解 比较累,做一道水题 还被卡常= = 我在\(ZOJ\)交过的两道\(NTT\)都被卡常了.. 哦,题意就是求第二类斯特林数,然后线段树维护一下集合数量就可以了 #inc ...

  3. Dll劫持漏洞详解

      一.dll的定义 DLL(Dynamic Link Library)文件为动态链接库文件,又称“应用程序拓展”,是软件文件类型.在Windows中,许多应用程序并不是一个完整的可执行文件,它们被分 ...

  4. Hadoop1重新格式化HDFS

    注意:原来的数据全部被清空了.产生了一个新的hdfs 1. 删除已有目录 1.1 查看hdfs-site.xml 将 dfs.name.dir和dfs.data.dir所指定的目录删除 vim hdf ...

  5. VirtualBox安装虚拟机全过程

    使用Virtual Box安装虚拟机,虚拟机操作系统使用CentOS7进行安装,安装完成后解决网络设置的问题. 一.虚拟机新建过程 1.点击新建. 2.设置内存大小,点击下一步. 3.选择虚拟硬盘,点 ...

  6. [DeeplearningAI笔记]序列模型3.2有条件的语言模型与贪心搜索的不可行性

    5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.2选择最可能的句子 Picking the most likely sentence condition lan ...

  7. Google Map API使用详解(一)——Google Map开发背景知识

    一.谷歌地图主页 谷歌地图对应不同的地区都会有一些专门的主页,首次登陆时会显示这些地区.比如,香港的:http://maps.google.com.hk,台湾的:http://maps.google. ...

  8. 其他:strtok和sscanf结合输入读取一行整数

    gets(buf); int v; char *p = strtok(buf," "); while(p) { sscanf(p,"%d",&v); p ...

  9. HDU 2582 规律 素因子

    定义$Gcd(n)=gcd(\binom{n}{1},\binom{n}{2}...\binom{n}{n-1})$,$f(n)=\sum_{i=3}^{n}{Gcd(i)}$,其中$(3<=n ...

  10. 用trigger触发datepicker

    jQuery UI的datepicker没有icon图片,工作需要,自己写了一个,原理是用div包裹住datepicker的input和一个button,隐藏掉input,而button被点击后也可以 ...