【HDU2222】Keywords Search(AC自动机)
Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
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.InputFirst line will contain one integer means how many cases will follow by.
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.OutputPrint how many keywords are contained in the description.Sample Input15shehesayshrheryasherhsSample Output3
4 5
she
he
say
shr
her
yasherhs 5
she
he
say
shr
her
yasherhs 3
she
she
she
shesheshe 6
she
he
he
say
shr
her
yasherhs 答案:
3
3
3
4
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 800100
#define Maxl 60 char s[Maxl];
char ss[]; struct node
{
int son[],cnt,fail;
}t[Maxn];int tot; void upd(int x)
{
t[x].cnt=;
memset(t[x].son,,sizeof(t[x].son));
} void read_trie()
{
int n;
scanf("%d",&n);
tot=;upd();
for(int i=;i<=n;i++)
{
int now;
scanf("%s",s+);
int len=strlen(s+);
now=;
for(int j=;j<=len;j++)
{
int ind=s[j]-'a'+;
if(!t[now].son[ind])
t[now].son[ind]=++tot,upd(tot);
now=t[now].son[ind];
if(j==len) t[now].cnt++;
}
}
} queue<int > q;
void build_AC()
{
int i,j,x,y;
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
x=q.front();
y=t[x].fail;
for(j=;j<=;j++)
{
if(t[x].son[j])
{
q.push(t[x].son[j]);
t[t[x].son[j]].fail=x?t[y].son[j]:x;
}
else t[x].son[j]=t[y].son[j];
}
q.pop();
}
} int gmark(int x,int y)
{
if(!x) return y;
if(t[x].cnt) y+=t[x].cnt,t[x].cnt=;
return gmark(t[x].fail,y);
} void ffind()
{
scanf("%s",ss+);
int l=strlen(ss+);
int now,ans=;
for(int i=;i<=l;i++)
{
int x=ss[i]-'a'+;
now=t[now].son[x];
ans+=gmark(now,);
}
printf("%d\n",ans);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
read_trie();
build_AC();
ffind();
}
return ;
}
[HDU2222]
2016-06-14 17:02:09
【HDU2222】Keywords Search(AC自动机)的更多相关文章
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- 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 ...
随机推荐
- javascript常用方法(慢慢整理)
获取类型:[object object],[object function],[object Undefined]等 Object.prototype.toString.apply(obj); 获取对 ...
- 从源码角度理解android动画Interpolator类的使用
做过android动画的人对Interpolator应该不会陌生,这个类主要是用来控制android动画的执行速率,一般情况下,如果我们不设置,动画都不是匀速执行的,系统默认是先加速后减速这样一种动画 ...
- oracle常用数据类型
oracle中常用数据类型分为三大类:
- 高效 jquery 的奥秘
当你准备使用 jQuery,我强烈建议你遵循下面这些指南: 1. 缓存变量 DOM 遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $( ...
- jstl中添加自定义的函数
由于jstl中提供的函数未必能够满足我们的要求,而我们又希望能够像jstl提供的函数那样能够轻松方便使用,那么可以通过自定义函数补充jsltl函数.给jstl添加自定义函数需要以下步骤: 定义一个st ...
- Intent.Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- pat_1008
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 读书笔记之 - javascript 设计模式 - 适配器模式
适配器模式可以用来在现在接口和不兼容的类之间进行适配. 使用这种模式的对象又叫包装器,因为他们是在用一个新接口包装另一个对象. 在设计类的时候往往遇到有些接口不能与现有api一同使用的情况,借助于适配 ...
- jQuery慢慢啃之工具(十)
1.jQuery.support//一组用于展示不同浏览器各自特性和bug的属性集合 2.jQuery.browser//浏览器内核标识.依据 navigator.userAgent 判断. 可用值: ...
- html5 拖拽的简要介绍
1,首先,你要告诉计算机那个元素可以拖动,或者是一张图,或者是一个盒子,在标签里面加上 draggable="true" 2,然后,监听该元素被拖动的函数 ondragstart ...