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 ...
随机推荐
- Mysql从客户端连接服务器连不上的问题
Mysql从客户端连接服务器连不上的问题 公司要用Mysql做一个测试,开始在自己的本地建一个Mysql数据库自己本地的程序再连上去,没有遇到过连接不上的问题.这次数据库在服务器上,从本地客户端连 ...
- jQueryMobile控件之展开与合并
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java中的HashSet和TreeSet
1:Set集合(理解) (1)Set集合的特点 无序,唯一 (2)HashSet集合(掌握) A:底层数据结构是哈希表(是一个元素为链表的数组) B:哈希表底层依赖两个方法:hashCode()和eq ...
- tornado 协程的实现原理个人理解;
tornado实现协程的原理主要是利用了(1)python里面的generator (2)future类和ioloop相互配合,两者之间的相互配合是通过gen.coroutine装饰器来实现的: 具体 ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- ThreadPoolExecutor机制探索-我们到底能走多远系列(41)
我们到底能走多远系列(41) 扯淡: 这一年过的不匆忙,也颇多感受,成长的路上难免弯路,这个世界上没人关心你有没有变强,只有自己时刻提醒自己,不要忘记最初出发的原因. 其实这个世界上比我们聪明的人无数 ...
- css模糊效果
CSS代码: .blur { filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */ -webkit-filter: blur(10px); ...
- 使用 CSS 去掉 iPhone 网页上按钮的超大圆角默认样式
使用 iPhone 上的浏览器去浏览网页的时候,按钮总是显示超大圆角的样式,显得超级恶心,但是我们自己定义 border-radius 为 0 也无法去除这个圆角,经过搜索发现这是 webikt 内核 ...
- dashboard
http://www.htmleaf.com/pins/chart-doc/index.html http://www.flotcharts.org/flot/examples/ https://gi ...
- 关于如何获取/清除 MAXScript 侦听器内的文本
关于如何获取/清除 MAXScript 侦听器内的文本 用来保存记录?还没想到实际用处,先记上. macroRecorder as string listener as stringclearList ...