Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 59827    Accepted Submission(s): 19715

Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
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.
Input
First line will contain one integer means how many cases will follow by.
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.
Output
Print how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs
AC自动机板子题
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
struct node{
int count;
node *fail;
node *next[];
node(){
fail=NULL;
count=;
memset(next,NULL,sizeof(next));
}
}*q[N];
char str[+];
char keyword[];
int head,tail;
node *root;
void insert(char *s){
node *p=root;
//cout<<3<<endl;
int len=strlen(s);
for(int i=;i<len;i++){
int v=s[i]-'a';
if(p->next[v]==NULL)p->next[v]=new node();
p=p->next[v];
}
p->count++;
// cout<<2<<endl;
}
void build_AC_automation(node *root){
root->fail=NULL;
head=,tail=;
q[head++]=root;
while(head!=tail){
node *p=NULL;
node *temp=q[tail++];
for(int i=;i<;i++){
if(temp->next[i]!=NULL){
if(temp==root)temp->next[i]->fail=root;
else{
p=temp->fail;
while(p!=NULL){
if(p->next[i]!=NULL){temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
q[head++]=temp->next[i];
}
}
}
}/*
int query(node *root){
int i=0,cnt=0,index,len=strlen(str);
node *p=root;
while(str[i]){
index=str[i]-'a';
while(p->next[index]==NULL && p!=root) p=p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp=p;
while(temp!=root && temp->count!=-1){
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
}
return cnt;
} */
int query(node *root){
int ans=;
//cout<<2<<endl;
int len=strlen(str);
node *p=root;
for(int i=;i<len;i++){
int v=str[i]-'a';
while(p->next[v]==NULL&&p!=root)p=p->fail;
p=p->next[v];
if(p==NULL)p=root;
node *temp=p;
while(temp!=root){
if(temp->count>=){
ans=ans+temp->count;
temp->count=-; }
else
break;
temp=temp->fail;
}
}
//cout<<2<<endl;
return ans;
}
int main(){
int n,t;
scanf("%d",&t);
while(t--){
root=new node();
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
gets(keyword);
insert(keyword);
}
build_AC_automation(root);
scanf("%s",str);
cout<<query(root)<<endl;
}
}
 
Sample Output
3

hdu 2222(AC自动机模版题)的更多相关文章

  1. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  2. HDU 2222 AC自动机(模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  5. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...

  6. hdu 3065 AC自动机模版题

    题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...

  7. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  8. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. Linux 查询PID和端口号

    https://www.cnblogs.com/understander/p/5546458.html

  2. R包

    查看默认安装包的位置 .libPaths() 移除包 remove.packages("package_name") 查看所有安装的包 library() 按 q 退出包列表   ...

  3. Ajax无刷新显示

    前台页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1. ...

  4. 微信小程序video监测播放进度

    video组件提供的进度相关的监测只有 bindtimeupdate ,官方说明这个函数250ms触发一次,在开发者工具上基本符合,但在真机上每隔1秒触发一次.达不到我们要求的精度.对比下音频,wx. ...

  5. Vue中this.$router.push参数获取(通过路由传参)【路由跳转的方法】

    传递参数的方法: 1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用nam ...

  6. ubuntu14.04禁用USB外存储设备

    ubuntu 14.04中禁用usb外存储设备: 在网上找了很多方法,大概都是下面的命令,而实际测试的时候没有什么作用. gsettings set org.gnome.desktop.media-h ...

  7. webpack打包出错,通过babel将es6转es5的出错。

    错误信息如下: 解决方法: 1,先安装babel-preset-es2015到项目中, cnpm install babel-preset-es2015 --save-dev2,在项目根目录中新建一个 ...

  8. Git ——Tool

    Git: 何为Git: Git 是一个可以实时记录文件变化.维护文件的安全的一个仓库! Git仓库是由** Linux 系统之父 Linus Torvalds ** 创建的一个开源 的软件!Githu ...

  9. 洛谷 2387 NOI2014魔法森林 LCT

    [题解] 我们先把边按照$a$值从小到大排序,并按照这个顺序加边. 如果当前要加入的边连接的两点$u$与$v$已经是连通的,那么直接加入这条边就会出现环.这时我们需要删除这个环中$b$值最大的边.因此 ...

  10. KD树学习小结

    几个月后的UPD: 学习完下面之后,实战中的总结: 0.比赛中正解就是kdtree的题目很少很少 1.几类优先考虑kdtree的题目: k(维度) >= 3 的题目 二维平面上涉及区间标记的题目 ...