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 ...
随机推荐
- [windows操作系统]windows管理
1.磁盘管理: 1.1.使用DISKPART命令行工具创建扩展分区: windows自带有一个disk management(磁盘管理)工具,但在其中却找不到如何创建扩展分区(一般MBR分区格式需要扩 ...
- Python开发入门与实战3-Django动态页面
3.Django动态页面 上一章节我们实现的helloworld视图是用来演示Django网页是创建的,它不是一个动态网页,每次运行/helloworld/,我们都将看到相同的内容,它类似一个静态HT ...
- JS三元
((productDatas[i].Img.indexOf("http") == -1) ? ("/upload/190-160/" + productData ...
- 1、webservice的简单使用
1.新建一个web端项目 2.点击添加项,选择web服务 3.在已经建好的项目中写一个方法 4.发布(发布方法选文件系统,web需要管理员权限) 生成文件夹: 5.配置IIS(略) 6.调用webse ...
- LNMP安装一键安装包
系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...
- JSONObject转换JSON--将Date转换为指定格式
项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...
- PCL常见编程问题
1.如何获取pcd文件点云里点的格式,比如是pcl::PointXYZ还是pcl::PointXYZRGB等类型? #include <pcl/io/pcd_io.h> #include ...
- 正向代理与反向代理的区别【Nginx读书笔记】
正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板, 简单的说, 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 这个代理服务器呢,他能访问那个我不能访问的网站 于是我 ...
- Artificial-Intelligence BOOKs
All of Statistics: A Concise Course in Statistical Inference The Elements of Statistical Learning:Da ...
- JSBinding + SharpKit / 编译 Cs 成 Js
轻轻一点菜单:[JSB | Compile Cs to Js] 主要产出:StreamingAssets/JavaScript/SharpkitGeneratedFiles.javascript,你的 ...