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 ...
随机推荐
- OMG点菜系统
算是一个项目吧,自己一个人也写了很久,有很多东西是自己写的,当然也有在网上借鉴人家大神的,不一而足,代码太多,不贴了,直接上图.(一直出现的天蓝色是使用windows自带主题壁纸上截取的一段) [原本 ...
- iOS移动下上传图片失败解决 (上传多图,带其他参数)
项目中有一个主要的功能,就是上传图片,结结果移动真的是很奇怪,WiFi,联通,电信都没有问题的情况下,居然在移动下不行,真的是很头疼.不过好在最后是解决了 项目的网络请求我是采用ASIHttpRequ ...
- NuSOAP与PHPRPC比较(转)
因为看到有人问 Nusoap 和 PHPRPC 的比较,为了让大家能够更清楚地了解 Nusoap 和 PHPRPC 的关系,所以在这里做一个简要的说明性介绍,所写的内容也不是面面俱到的,只写了一些主要 ...
- chromium 一些设置 --插件安装
一.安装flash插件 打开网页 http://get.adobe.com/cn/flashplayer/otherversions/ 选择如图 所示 fp 18 for Opera and Chr ...
- GFT_News Auto
using AnfleCrawler.Common; using Newtonsoft.Json.Linq; using System; using System.Collections.Generi ...
- Android SQLiteOpenHelper(二)
上一篇我们已经了解了SQLiteOpenHelper 和 构造函数. 现在我们就来掌握一下:onCreate( ) onUpgrade( ) onDowngrade( ) public void ...
- 【php常用】常用函数啥的
1.intval() 把变量转换成整数类型 2.trim() 去除字符串两边空格or 加上参数是去除该参数 3.array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名 ...
- poj2387 spfa求最短路
//Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...
- 2016 - 1 - 24 NSURLSession (一)
一: NSURLSession简介 1.实施步骤 1.1 使用 NSURLSession对象 创建TASK ,然后执行TASK 2.TASK的类型: 二: NSURLSession的简单使用: - ( ...
- ExpandableListView的OnitemLongclickListener事件
expandableListView是带分组的Listview,通常会有setOnChildClickListener,setOnGroupClickListener,但如果是长按的事件,可以用以下方 ...