HDOJ2222 Keywords Search-AC自动机
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.
现在才知道{scanf("%s",buf);ac.insert(buf);}和scanf("%s",buf),ac.insert(buf);不一样 TLE了十几次 一直在优化IO 以后要乖乖写分号了...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
;
;
struct AC{
int size,root,next[maxn][maxc],fail[maxn],end[maxn];
int newNode(){
//memset(next[size],-1,sizeof(next[size]));
;i<maxc;i++) next[size][i]=-;
end[size]=;
++size;
;
}
int idx(char c){
return c-'a';
}
void init(){
size=;
//memset(end,0,sizeof(end));
root=newNode();
}
void build(){
queue<int> Q;
fail[root]=root;
;i<maxc;i++){
) next[root][i]=root;
else fail[next[root][i]]=root,Q.push(next[root][i]);
}
while(!Q.empty()){
int u=Q.front();Q.pop();
;i<maxc;i++){
) next[u][i]=next[fail[u]][i];
else fail[next[u][i]]=next[fail[u]][i],Q.push(next[u][i]);
}
}
}
int query(char* s){
;
int l=strlen(s);
;i<l;i++){
n=next[n][s[i]-'a'];
int trav=n;
while(trav!=root){
count+=end[trav];
end[trav]=;
trav=fail[trav];
}
}
return count;
}
void insert(char* s){
int n=root;
int l=strlen(s);
;i<l;i++){
) next[n][s[i]-'a']=newNode();
n=next[n][s[i]-'a'];
}
end[n]++;
}
};
AC ac;
*maxn];
int main()
{
int T;scanf("%d",&T);
while(T--){
int n;scanf("%d",&n);
ac.init();
;i<n;i++) {
scanf("%s",buf);ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
;
}
HDOJ2222 Keywords Search-AC自动机的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 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 ...
- 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 ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
随机推荐
- win7下用python3.3获取cable modem的设备信息
毕业一年多了,一直做cable modem的测试,总是觉得在国内这一行的人才很少,想找个师傅真的很不容易. 苦闷了许久之后,终于决定,自己去写点东西,万一就找到同行了呢? 下面就是本小姐写的第一篇博客 ...
- BEGIN_TEMPLATE_MESSAGE_MAP
最近转做服务端开发,或多或少有点坑爹的感觉.目前正在恶补Linux C/C++编程,主要还是集中在Linux系统API的学习.不过也好,以后更新的内容不仅仅只有Windows了. 今天说一点简单的东西 ...
- JS中数据类型及原生对象简介
js是一种专门设计用来给网页增加交互性的编程语言,它的技术体系包含了一下几个方面: 1.JavaScript核心语言定义:包括数据类型,变量,常量,运算符,语句等. 2.原生对象和内置对象 3.浏览器 ...
- HDU2222
http://acm.hdu.edu.cn/showproblem.php?pid=2222 注意: 1. keyword可以相同,因此计算时要累计:cur->num++. 2. 同一个keyw ...
- javascript笔记6-DOM
DOM(文档对象模型)是针对HTML和XML文档的一个API.DOM描绘了一个层次化的节点树,允许程序员添加.修改页面的一部分. 节点层次:DOM可以将任何HTML或XML文档描绘成一个由多层次节点构 ...
- jQuery里ajax的用法
$.ajax({ type:'post',//这里页面数据发送请求的方式可以为post和get cache:'false ', //这里可以为false或者true 是否要缓存 ,默认为false u ...
- eclipse working sets 视图 解决Other Projects不见问题
请移步: http://note.youdao.com/yws/public/redirect/share?id=d54cac4232078f9acab551d62337a2d1&type=f ...
- mac下XAMPP服务器配置多站点配置局域网配置 (转)
原文:http://blog.csdn.net/wbw1985/article/details/9493989 Mac 上的软件大多是收费的,配置开源的东东也挺麻烦,网上搜索发现XAMPP软件是集成了 ...
- 12-8下午 php语法
<?php //var_dump(empty($a)); //判断变量是否为空//var_dump(isset($a)); //判断变量是否定义//$a = 10;//unset($a); // ...
- Qt控件篇 ---- QTableView/QTableWidget
记录 //按字母排序 item->setText("2"); //按数值排序item->setData(Qt::DisplayRole, 2);