hdu 2222 Keywords Search 模板题
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 85068 Accepted Submission(s): 29646
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.
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.
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#define ll long long
using namespace std;
int tree[][],vis[],fail[];
int t,n,cnt,id,root,num=;
string s,ss; void insert()//建树
{
root=;
for(int i=;s[i];i++)
{
id=s[i]-'a';
if(tree[root][id]==)
tree[root][id]=++num;
root=tree[root][id];
}
vis[root]++;//单词结尾标记
} void build()//构建失配指针
{
queue<int>p;
for(int i=;i<;i++)
{
if(tree[][i])//将第二行所有出现过的字母的失配指针指向root节点0
{
fail[tree[][i]]=;
p.push(tree[][i]);
}
} while(!p.empty())
{
root=p.front();
p.pop();
for(int i=;i<;i++)
{
if(tree[root][i]==)//没有建树,不存在这个字母
continue;
p.push(tree[root][i]);
int fa=fail[root];//fa是父亲节点
while(fa&&tree[fa][i]==)//fa不为0,并且fa的子节点没有这个字母
fa=fail[fa];//继续判断fa的父亲节点的子节点有没有这个字母 fail[tree[root][i]]=tree[fa][i];//找到就构建失配指针 }
}
} int search(string ss)//查找
{
root=,cnt=;
for(int i=;ss[i];i++)
{
id=ss[i]-'a';
while(root&&tree[root][id]==)//失配转移
root=fail[root]; root=tree[root][id];
int temp=root;
while(vis[temp])
{
cnt=cnt+vis[temp];
vis[temp]=;//清除标记,避免重复
temp=fail[temp];
}
}
return cnt;
}
int main()
{
cin>>t;
while(t--)
{
memset(tree,,sizeof(tree));
memset(vis,,sizeof(vis));
cin>>n;
for(int i=;i<n;i++)
{
cin>>s;
insert();
}
build();
cin>>ss;//文本串
cout<<search(ss)<<endl;
}
}
hdu 2222 Keywords Search 模板题的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
- 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 ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
随机推荐
- SQL mybatis动态查询小结
动态返回mysql某张表指定列的名字 <select id="queryColumns" resultType="map" parameterType=& ...
- Docker将自己的镜像发布到个人私有仓库
Docker将自己的镜像发布到个人私有仓库 1.注册dockerhub账户 docker提供了一个类似于github的仓库dockerhub, 网址https://hub.docker.com/需要注 ...
- C++代码如何附加到C#写的主程序中?
背景是这样:C#的exe程序,C#中调用C++的dll,也就是所谓的托管吧. C#的exe出了点问题,想在C++的dll的源码中调试,附加到进程时,加了断点,怎么也跟不进去.断点不变红啊,急死了. 最 ...
- SQL SERVER 2005还原差异备份、日志备份 2012-03-29 11:43
其实要备份,还原最安全最有保障的是完全备份.但是完全备份肯定是需要更多的磁盘空间的开销.尤其是数据量比较大的.比如基数是500M,每天的增长量为10M,那么第一次完全备份是500M,第二次是510M, ...
- centOS+DJango+mysql_nginx部署流程记录
安装Python3.6.2: https://www.jianshu.com/p/7a76bcc401a1 安装MySQL: https://www.cnblogs.com/luohanguo/p/9 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:引用(Blockquote)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 将一行很长的js代码格式化输出方便查看
之前的一行js代码,有2万多字符,打开这个网址,粘贴到左边空白框,点下面格式化: 参考下面文章: 数千行的js代码变成了一行,如何复原,该换行的换行,该对齐的对齐_开发工具_小邯韩的博客-CSDN博客 ...
- 【原】postman常用设置全局变量的js片段
postman知识总结: API自动化利器:http://www.bayescafe.com/tools/use-postman-to-test-api-automatically.html 1.获取 ...
- C 语言入门---第十一章---C语言重要知识点补充
====C语言typedef 的用法==== 1. C语言允许为一个数据类型起一个新的别名,就像给人起绰号一样. typedef OldName newName; typedef 和 #define ...
- java获取指定月份有几个星期x,获取指定月份跨了多少个星期
例如获取2020年5月一共有多少个星期二,一共跨了多少个星期 public class MainTest { public static void main(String[] args) throws ...