Keywords Search HDU - 2222 ( ac自动机)模版题
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 77903 Accepted Submission(s): 27032
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<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
const int maxn=;
struct Trie{
int next[][],fail[],end[];
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 temp=now;
while(temp!=root){
res+=end[temp];
end[temp]=;
temp=fail[temp];
}
}
return res;
}
};
char buf[];
Trie ac;
int main()
{
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 ;
}
Keywords Search HDU - 2222 ( ac自动机)模版题的更多相关文章
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- 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自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
随机推荐
- AOP的XML实现方式
与注解方式类似,只不过所有设置是通过xml来设置 // 切面类 public class Aop { public void around(ProceedingJoinPoint pjp) throw ...
- Shell笔试题3
1.查找当前目录中所有大于500M的文件,把这些文件名写到一个文本文件中,并统计其个数.find ./ -size +500M -type f | tee file_list | wc -l 2.在目 ...
- SpringBoot | 第三章:springboot配置详解
基于springboot的约定优于配置的原则,在多数情况下,启动一个应用时,基本上无需做太多的配置,应用就能正常启动.但在大部分开发环境下,添加额外配置是无所避免的,比如自定义应用端口号(比较在机器比 ...
- Visual Studio中的引用项目和直接引用DLL文件
在VS中引用类库时有多种方法,其中用的最多的就是在引用时选择项目选项卡引用本解决方案下的类库项目和选择浏览选项卡直接引用类库DLL文件,实际上这两种引用方式略有不同,今天就为大家总结下. C#本地项目 ...
- 使用Kubernetes的java-client实现Deployment的部署及更新操作
1. 背景介绍 需求: 针对多种协议SDK构造探针,测试公司接入机服务状况(每一个探针应对单一接入机,接入机数量可能会动态变化). 难点: 大多数协议SDK均不支持多实例运行,且部分SDK通过生成文件 ...
- JavaWeb前端笔记
day06 回顾: bootstrap: css框架,html/css/js集于一身,ie 6/7/8兼容有问题 开发响应式页面,使用于不同的上网设备 使用步骤: 1.导入bootstrap.css ...
- Android 使用GreenDao 添加字段,删除表,新增表操作
GreenDao 给我个人感觉 比一般的ORM框架要好很多,虽然说上手和其他的比起来,较复杂,但是如果使用熟练以后,你会爱上这个框架的 用这些ORM 框架给我的感觉都是,当升级时,都需要进行数据库所有 ...
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法
如果已经启动了四个Activity:A,B,C和D.在D Activity里,我们要跳到B Activity,同时希望C finish掉,可以在startActivity(intent)里的inten ...
- Python学习笔记-day1(if流程控制)
在python中,流程控制语句为强制缩进(4空格) if username=='lmc' and password=='123456': print('Welcome User {name} logi ...
- 【转】【C++】【MFC】关于RADIO BUTTON的使用方法
*原文地址:http://blog.csdn.net/c_cyoxi/article/details/23868979 1. 环境:VS2010 2. 分组 将radio1.radio2.radio3 ...