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).问匹配串中含有哪几种模 ...
随机推荐
- vim之补全2(完全个人定制版)
关于补全的方面要说的的确很多, 这里选择分为两个章叙述. 如果你想学vim, 你需要有很强的耐心, 如果你想锻炼这种耐心, 你可以试着先看完我之前的文章. 好了, 下面继续我们的vim补全吧. vim ...
- 如何创建一个项目,让gitlab自动触发jenkins进行构建
前进是:你已经配置好jenkins+gitlab自动化布置了,这里只是常规构建新的项目时,需要做的配置,记录下来,以免忘了又着急 参考这篇博客: https://www.jianshu.com/p/e ...
- scss基础
1.变量$ 全局 局部 .div{ $color:yellow; } 2.类似函数@mixin border-radius($radius) { }引用:@include border-radius( ...
- 如何从源码启动和编译IoTSharp
IoTSharp 项目是一个开源物联网平台,数据库使用PostgreSQL , 后端使用 Asp.Net Core 2.2 ,前端使用 vue-element-admin , 下面我们介绍如何启动项 ...
- 04 学习java养成良好的写作习惯
1, 驼峰命名法 首字母大写 2, 写的时候大小中括号都补全,不忘记分号 不要都放在一行上 3, 缩进对其,tab键 4, 严格要求自己,养成良好的写作风格 5, javadoc可以将文档注释,直接生 ...
- 小白年薪24万,为什么Linux运维工程师薪资这么高?
借了云计算的东风,Linux岗位这几年是越来越火,特别是Linux云计算运维工程师,如今早已成为互联网的核心岗位之一,薪资待遇飞快的上涨. 作为一个细分的专业岗位,Linux云计算工程师由于其入门学习 ...
- 快速搭建vue2.0+boostrap项目
一.Vue CLI初始化Vue项目 全局安装vue cli npm install --global vue-cli 创建一个基于 webpack 模板的新项目 vue init webpack my ...
- Linux - redis主从同步
目录 Linux - redis主从同步 环境准备 配置主从同步 测试写入数据,主库写入数据,检查从库数据 手动进行主从复制故障切换 Linux - redis主从同步 原理: 从服务器向主服务器发送 ...
- BZOJ 2501 [usaco2010 Oct]Soda Machine
[题意概述] 给出一个[0,1,000,000,000]的整数数轴,刚开始每个位置都为0,有n个区间加操作,最后询问数轴上最大的数是多少. [题解] 我写的是离散化后线段树维护区间最值. 其实貌似不用 ...
- c# SQL事务
SQL事务执行 SqlTransaction sqlTransaction = sqlConnection.BeginTransaction(); SqlCommand sqlC ...