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 ...
随机推荐
- 【读书笔记】图解HTTP完整流程
1.1 使用 HTTP 协议访问 Web: 根据 Web 浏览器地址栏中指定的URL,从服务端获取资源等信息,在页面上展示. Web 使用一种 HTTP (超文本传输协议)作为规范,完成从客户端到服务 ...
- (0)Lora及LoraWAN
Lora和LoraWAN的区别 LoRa经常被误用来描述整个LPWAN通信系统,其实Lora是Semtech拥有的专有调制格式. SX1272和SX1276 LoRa芯片使用称为chirp扩频(CSS ...
- 06-常见的RAID技术
目录 06-常见的RAID技术 参考 RAID基本概念 RAID数据组织形式 RAID数据保护方式 RAID0 RAID1 RAID5 RAID6 混合RAID - RAID 10 06-常见的RAI ...
- nginx 性能优化的概述及在CPU资源方面的处理
nginx的性能优化的概述 软件层面的提升硬件的使用率 增大CPU的利用率 增大内存的利用率 增大磁盘IO利用率 增大网络带宽利用率 提升硬件规格 网卡:万兆网卡.例如10G.25G.40G等 磁盘: ...
- nginx 反向代理memcached、websocket及nginx文件方面的优化
安装memcached服务,并启动添加数据 yum -y install memcached systemctl start memcached.service 启动 [root@python ~]# ...
- Go Start
一.安装 下载解压后,配置PATH tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz export PATH=$PATH:/usr/local/go ...
- java模式之单例
懒汉式:需要实例的时候new public class Singleton_Lazy { private static Singleton_Lazy mSingleton; private Singl ...
- LeetCode 101.对称二叉树 - JavaScript
题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...
- maven缺失ojdbc6解决方案 :Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错
问题已解决,感谢博主,给您磕头了. | | 解决方法就是把缺少的 jar 手动添加到本地仓库中,再重新引入依赖即可.详情请参考以下链接. | | 转发自: https://blog.csdn.net/ ...
- Linux下安装Docker,报错docker: unrecognized service的两种解决方案
转自(方法1):https://www.cnblogs.com/ECJTUACM-873284962/p/9362840.html