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 ...
随机推荐
- 360网安学习笔记——Web安全原理与实践
网络安全 基本技能: 1.编程语言 2.计算机网络 3.操作系统 4.office 专业技能 1.web安全 2.网络安全 3.渗透测试 4.代码审计 能力提升 1.书籍 2.站点 3.安全平台 We ...
- HttpClient和HtmlUnit的比较总结以及使用技巧
本文转自: https://blog.csdn.net/zstu_cc/article/details/39250903 https://blog.csdn.net/zstu_cc/article/d ...
- 通过js检测浏览器支持的字体,从而显示支持的字体,让用户选择。
http://www.zhangxinxu.com/wordpress/2018/02/js-detect-suppot-font-family/ 本文根据张鑫旭文章. 字体函数: var dataF ...
- spring aop @after和@before之类的注解,怎么指定多个切点
有如下两个切点: @Pointcut("execution(public * com.wyh.data.controller.DepartmentController.*(..))" ...
- 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用
转 单元测试及框架简介 --junit.jmock.mockito.powermock的简单使用 2013年08月28日 14:33:06 luvinahlc 阅读数:6413 标签: 测试工具单元测 ...
- Do You Know These Plastic Bottle Processing Terms?
The molding process of a plastic bottle refers to a process of making a final plastic article from a ...
- idea没有import project解决办法
参考:https://blog.csdn.net/zengxiaosen/article/details/52807540
- 在webView中的返回键
在写webView中我们按一下返回键,退到上一个我们浏览的网页,到第一个页面时,按两下退出程序,且按一下时提示你在按一下退出程序 只要加上这个方法即可 public void onBackPresse ...
- python打印日志log
整理一个python打印日志的配置文件,是我喜欢的格式. # coding:utf-8 # 2019/11/7 09:19 # huihui # ref: import logging LOG_FOR ...
- TensorFlow样例一
假设原函数为 f(x) = 5x^2 + 3,为了估计出这个函数,定义参数未知的函数g(x, w) = w0 x^2 + w1 x + w2,现要找出适合的w使g(x, w) ≍ f(x).将这个问题 ...