HDU-2222文字检索
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.
InputFirst 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.
OutputPrint how many keywords are contained in the description.Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
题意就是说给你一些模式串,问你有哪几个在目标串中出现过,(⊙o⊙)…,和洛谷那道末班体一模一样,就是多了一个多组数据。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; int n,cnt=;
char s[];
struct Node
{
int c[],suf,flag;
void init()
{
suf=flag=;
memset(c,,sizeof(c));
}
}tree[]; void init()
{
for (int i=;i<=cnt;i++)
tree[i].init();
cnt=;
}
void Ins()
{
int head=,l=strlen(s);
for (int i=;i<l;i++)
{
int now=s[i]-'a';
if (!tree[head].c[now]) tree[head].c[now]=++cnt;
head=tree[head].c[now];
}
tree[head].flag++;
}
void Make_AC()
{
for (int i=;i<;i++) tree[].c[i]=;
int head=,tail=;
queue<int>q;
while (!q.empty()) q.pop();
tree[].suf=;
q.push();
while (!q.empty())
{
int u=q.front();
q.pop();
for (int i=;i<;i++)
if (tree[u].c[i])
{
tree[tree[u].c[i]].suf=tree[tree[u].suf].c[i];
q.push(tree[u].c[i]);
}
else tree[u].c[i]=tree[tree[u].suf].c[i];
}
}
void Solve()
{
scanf("%s",s);
int ans=,head=,len=strlen(s);
for (int i=;i<len;i++)
{
int now=s[i]-'a';
head=tree[head].c[now];
for (int j=head;j&&tree[j].flag!=-;j=tree[j].suf)
ans+=tree[j].flag,tree[j].flag=-;
}
printf("%d\n",ans);
}
int main()
{
int Cas;
scanf("%d",&Cas);
while (Cas--)
{
init();
scanf("%d",&n);
while (n--){scanf("%s",s);Ins();}
Make_AC();
Solve();
}
}
HDU-2222文字检索的更多相关文章
- HDU 2222 AC自动机模板题
1.HDU 2222 2.题意:给出n个单词,一个字串,求有多少个单词在字串里出现了.注意给出的单词可能会重复,重复的不计. 3.总结:入门题.在查询这里还是不太懂. #include<bits ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 最简单的AC自动机套模板应用
HDU 2222 题意:给出N(N<=10,000)个单词,每个单词长度不超过50.再给出一个字符串S,字符串长度不超过1,000,000.问有多少个单词出现在了字符串S中.(单词可能重复,单词 ...
- HDU 2222 (AC自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- hdu 2222 Keywords Search
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...
随机推荐
- Java内存模型_基础
线程之间的通信机制有两种: 1.共享内存:线程之间共享程序的公共状态,通过写-读内存中的公共状态进行隐式的通信. 2.消息传递:线程之间没有公共状态,线程之间必须发送消息来显示的进行通信 同步:是指程 ...
- 通过css控制超链接不显示下划线
“页面属性”——“链接”——“下划线样式”——“始终无下划线” <style type="text/css"> a:link { text-decoration: no ...
- 在JS事件封装时,addEventListener()方法的this问题
最近在写js的类库,模仿的是jquery的编程风格,当封装到事件监听的时候发现遇到了一个问题,代码是这样的: 上面是封装的一个事件委托的代码,我以为上面的封装跟普通的事件监听一样简单,结果我在调用时发 ...
- 搭建互联网DNS构架
author:JevonWei 版权声明:原创作品 构建"." DNS."com" DNS及"danran.com",用户通过"电 ...
- ASP.NET Core 运行原理解剖[5]:Authentication
在现代应用程序中,认证已不再是简单的将用户凭证保存在浏览器中,而要适应多种场景,如App,WebAPI,第三方登录等等.在 ASP.NET 4.x 时代的Windows认证和Forms认证已无法满足现 ...
- plsql developer 恢复默认布局界面
tools-preferences-appearance-(reset docking,reset toolbars)
- Linq 实现sql中的not in和in条件查询
T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Where CategoryID in (1, 2) T-S ...
- VS2012启用angularjs智能提示Intelligence(转)
1. 關閉正在執行的 Visual Studio 2012 2. 開啟檔案總管,並進入以下目錄: C:\Program Files (x86)\Microsoft Visual Studio 11.0 ...
- Maven(四)之Maven在IntelliJ IDEA的配置与使用
前面介绍了Maven的一些基本知识,这一篇讲一下在IntelliJ IDEA配置和使用Maven 一.配置Maven 在File->Settings->Build,Execution,De ...
- ps图层面板上的【透明度】与【填充】的区别
为文字添加投影,分别调图层面板上的[透明度]与[填充]的值你就知道区别了. 如上图降低填充的数值,结果只对文字颜色有影响却对投影毫无影响. 而如上图,调整不透明度的时候对文字颜色与投影均产生效果. 这 ...