hdu 2222(AC自动机模版题)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 59827 Accepted Submission(s): 19715
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.
#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;
}
}
hdu 2222(AC自动机模版题)的更多相关文章
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
随机推荐
- ArcGIS Android工程迁移到其他电脑不能打开的问题
问题描述:当我把已经做好的ArcGIS Android工程想在其他电脑运行时,总是会提示报错.而报错的地方,正是出现在下面这条语句上. compile 'com.esri.arcgisruntime: ...
- unity之Rigidbody属性
Rigidbody属性 Mass表示物体的质量,数值类型为float,默认值为1.大部分物体的质量属性接近于0.1才符合日常生活感官感受,超过10 ,则失去了仿真效果. Drag表示平移阻力,其数值类 ...
- Floyd模板
比较简单的算法:但是当点太多需要剪枝,不然很耗时 void Floyd() { ;k<n;++k) ;i<n;++i) ;j<n;++j) dj[i][j] = min(dj[i][ ...
- Qt 窗体间传值(代码备份)
刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Lab ...
- UICollectionView(一)基本概念
整体预览 高等级的包含和管理(Top-level containment and management) UICollectionView UICollectionViewController UIC ...
- vue编辑回显问题
真是疯了,vue怪毛病真多 就下面这玩意儿,多选组合框,新增的时候好用的不行不行的,到了编辑的时候,要回显数据,怪毛病一堆一堆的 首先,回显的时候要传一个数组,但是这个数组里的元素得是字符串类型的,如 ...
- 发布自己的nuget包
1.先到www.nuget.org注册账户,然后在用户中心获取apikey 2.到https://dist.nuget.org/index.html下载最新的nuget.exe,放到你的项目根目录下 ...
- 为什么阿里规约手册要求谨慎使用Arrays.asList方法
前言 在开发中,有时候会碰到把多个参数,或者说把数组转成List的需求,通常我们会使用 Arrays.asList()方法.但是该方法在使用的过程中,稍有不慎就会出现严重的异常.有如下代码: @Tes ...
- 怎么选择最适合自己的Python培训机构?
Python培训已经成为入门Python的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Python培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部 ...
- Python random模块&string模块 day3
一.random模块的使用: Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. 1.常用函数: (1)random.random() 用于生成一个0到1 ...