hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222
题目:
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 56558 Accepted Submission(s): 18493
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.
5
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std; struct AC_auto
{
const static int LetterSize = ;
const static int TrieSize = * ( 1e5 + ); int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize]; int newnode(void)
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = ;
return tot++;
} void init(void)
{
tot = ;
root = newnode();
} int getidx(char x)
{
return x - 'a';
} void insert(char *ss)
{
int len = strlen(ss);
int now = root;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
if(next[now][idx] == -)
next[now][idx] = newnode();
now = next[now][idx];
}
end[now]++;
} void build(void)
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < LetterSize; i++)
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root,Q.push(next[root][i]);
while(Q.size())
{
int now = Q.front();Q.pop();
for(int i = ; i < LetterSize; 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 match(char *ss)
{
int len,now,res;
len = strlen(ss),now = root,res = ;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
int tmp = now = next[now][idx];
while(tmp)
{
res += end[tmp];
end[tmp] = ;//
tmp = fail[tmp];
}
}
return res;
}
void debug(void)
{
for(int i = ;i < tot; i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = ;j < LetterSize;j++)
printf("%3d",next[i][j]);
printf("]\n");
}
}
}; AC_auto ac;
char sa[];
int main(void)
{
int t,n;
scanf("%d",&t);
while(t--)
{
ac.init();
scanf("%d",&n);
while(n--)
scanf("%s",sa),ac.insert(sa);
ac.build();
scanf("%s",sa);
printf("%d\n",ac.match(sa));
} return ;
}
hdu2222 Keywords Search ac自动机的更多相关文章
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
随机推荐
- HTML(.js) – 最简单的方式操作 DOM 的 JS 库
HTML(.js) 是一个轻量的(压缩后~2kb) JavaScript 库,简化了与 DOM 交互的方法. 这个 JavaScript 库的方法可读性很好,并具有搜索和遍历 DOM 的方法.相比 j ...
- jQuery动态提示消息框效果
效果预览:http://keleyi.com/keleyi/phtml/jqtexiao/2.htm 原文:http://keleyi.com/a/bjac/hxv86dyi.htm <!DOC ...
- 分享11个纯css完成的图片浏览器
图片画廊用于在网站上显示系列图片,它已成为网站重要的组成部分.实现图片画廊有很多种方法,今天要与大家分享的是11个使用纯 CSS 实现的图片画廊,它们代码少,效果炫,加载速度快,希望能对大家有所帮助. ...
- 设置跨域的iframe的高度
原因 如下图,A域中有个B域的页面,但是B的页面的长度不确定,A无法去设置一个准确的高度. PS:iframe高度设置auto是无效的 解决办法 如上图, (1)在B页面中加一个A的代理页面的ifra ...
- Atitit.git的存储结构and 追踪
Atitit.git的存储结构and 追踪 1. Add index.js 2 index1 1.1. new1 1.2. Modify1 2. Commit1 1. Add index.js 2 i ...
- IOS安全测试
1.本地存储安全 配置文件 缓存 数据库 测试数据 证书数据 2.网络通信安全 http明文通信 https证书认证 敏感参数弱加密 加签/验签策略 外接第三方SDK 外发不明数据 3.源代码安全 日 ...
- XMPP学习——1、介绍
XMPP(Extensible Messaging and Presence Protocol,前称Jabber[1])是一种以XML为基础的开放式实时通信协议,是经由互联网工程工作小组(IETF)通 ...
- 关于OC中的block自己的一些理解(一)
一.关于block 1.block的作用:保存一段代码. 2.苹果官方推荐的一种语法,类似于C语言的函数,但是比函数更加灵活. 3.^是block语法的标识. 二.block的用法 1)无返回值无参数 ...
- 软件工程(C编码实践篇)课程总结
课程内容来自网易云课堂中科大孟宁老师的软件工程(C编码实践篇)课程. 课程页面 我觉得本门课程的设置非常科学,每一周课程都是基于上一周课程的进一步抽象,使得学习者能够循序渐进,逐渐加深对软件工程的理解 ...
- select接收后台返回值的解决方案
在做页面表单或者条件筛选的时候,如何把select标签的值,在刷新页面后,保持选择的值.下面,将给出两种解决方案: 前提: 前台select标签 name为type : 后台接收type的值,业务完成 ...