HDU2222 Keywords Search [AC自动机模板]
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50435 Accepted Submission(s): 16233
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
she
he
say
shr
her
yasherhs
把kmp和trie树组合起来,就是AC自动机。利用fail指针在失配时,在trie树中寻找上一个匹配点
目前并没有完全弄懂。理解大概还要好久
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct ACa
{
int next[][];
int fail[],end[];
int root,cnt;//根结点,计数器
int newnode(){
for(int i=;i<;i++){
next[cnt][i]=-;//子结点设为空
}
end[cnt++]=;//以该结点结束的串数
return cnt-;
}
int clear(){//初始化
cnt=;//结点数重置
root=newnode();//初始化根结点 //完成后root=0
}
int insert(char c[]){
int now=root;
int len=strlen(c);
for(int i=;i<len;i++){
if(next[now][c[i]-'a']==-)//如果对应结点未建立,添加结点
next[now][c[i]-'a']=newnode();
now=next[now][c[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];//链接到fail指针对应结点的后面
else{
fail[next[now][i]]=next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
int query(char c[]){//查询
int len=strlen(c);
int now=root;
int res=;
for(int i=;i<len;i++){
now=next[now][c[i]-'a'];
int temp=now;
while(temp!=root)
{
res+=end[temp];
end[temp]=;
temp=fail[temp];
}
}
return res;
}
};
char buf[];
ACa ac;
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
ac.clear();
int i;
for(i=;i<=n;i++){
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}
HDU2222 Keywords Search [AC自动机模板]的更多相关文章
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- 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自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
随机推荐
- (转载)java多态(2)-------Java转型(向上或向下转型)
5.13.1 向上转型 我们在现实中常常这样说:这个人会唱歌.在这里,我们并不关心这个人是黑人还是白人,是成人还是小孩,也就是说我们更倾向于使用抽象概念“人”.再例如,麻雀是鸟类的一种(鸟类的子类), ...
- 06Spring_使用注解配置bean对象
Spring注解开发需要jar包 和 xml开发 一样的 ! 第一步: 新建项目, 导入jar包(就是前一篇文章里面的那几个核心jar包) 第二步: 在需要spring创建对象类上面 添加@Compo ...
- ie6,ie7兼容性总结(转)
其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致的,因此,我们可以进行一些初始化,很多问题都很轻松解决. 下面是14条特殊情况仅供参考: 1. 文字本身的大小不兼容.同样是font ...
- customized English word breaker for sql server 2008
Open the Registry Editor, by: Clicking Start, and clicking Run. In the Run dialog box, in the Open b ...
- 使用地址栏访问CXF Webservice写法
/* * 通过url调用 * http://localhost:8080/EFP/webService/TestWebservice/testOut/arg0/liuyx */ http://loca ...
- 深入理解abstract class和interface(转)
原文地址 深入理解abstract class和interface java提高篇(四)-----抽象类与接口
- DEDECMS中直接通过数据库插入文章
突然想用爬虫爬来的数据对接DEDECMS,所以研究了一下怎么直接在DEDECMS中直接插入一片文章 (但其实我并没有这样用.因为我直接用前台做的网页,用ajax请求的json数据,其他的都简单,就是没 ...
- 把字符串添加到HashMap中
&ZhuoTai_Name=205&NoSongDanDish=0&OrderZhuoTai_ID=aca87b77797e4c859a53c228471a2636&Z ...
- 基于DDD的.NET开发框架 - ABP初探
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...