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 ...
随机推荐
- Android重要控件———ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- 简单poi读取excel
1.添加依赖jar包 maven配置: <!-- poi being --> <dependency> <groupId>org.apache.poi</gr ...
- inline-block
在CSS中,块级对象元素会单独占一行显示,多个block元素会各自新起一行,并且可以设置width,height属性:而内联对象元素前后不会产生换行,一系列inline元素都在一行内显示,直到该行排满 ...
- Android 学习第6课,循环功能
package ch02; public class jiujiuchengfa { public static void main(String[] args) { // TODO 自动生成的方法存 ...
- C++嵌入Python,以及两者混用
以前项目中是C++嵌入Python,开发起来很便利,逻辑业务可以放到python中进行开发,容易修改,以及功能扩展.不过自己没有详细的研究过C++嵌入python的细节,这次详细的研究一下.首先我们简 ...
- 摘要评注The Cathedral & The Bazaar
2013年暑期买到这本书,距离其第一版已经有14年之久,而最早发布在互联网上的文章更是早在1997年.在我阅读的时候,很多事迹已经沉积为历史,很多预言已经成为现实.而这本书的意义却丝毫没有因此淡化,反 ...
- /proc/uptime详解
From:http://smilejay.com/2012/05/proc_uptime/ 在Linux中,我们常常会使用到uptime命令去看看系统的运行时间,它与一个文件有关,就是/proc/up ...
- JPush极光推送Java服务器端API
// 对android和ios设备发送 JPushClient jpush = new JPushClient(masterSecret, appKey); // 对android和ios设备发送 ...
- 初学Html
HTML,超文本标记语言,文本解释性语言,它的源代码不通过编译而直接在浏览器中运行时被翻译,其决定的是网页的结构和内容. 一谈html,自然首先是浏览器,比较具有代表性的浏览器则是谷歌.火狐.IE.苹 ...
- android之ViewPager
在android中ViewPager是非常常用的控件.它在android.support.v4.view.ViewPager下.你们自己可以进http://developer.android.com/ ...