ac自动机(tree+kmp模板)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 85955 Accepted Submission(s): 29888
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.
5
she
he
say
shr
her
yasherhs
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
using namespace std;
const int N = ;
char a[N] , str[];
int k = , tree[][];
int color[];
int fail[];
int ans = ;
void winsert(char *a)
{
int p = ;
for(int i = ; a[i] ; i++)
{
int c = a[i] - 'a';
if(!tree[p][c]) tree[p][c] = ++k;
p = tree[p][c];
}
color[p]++;
} void getfail()
{
queue<int>q;
for(int i = ; i < ; i++)
{
if(tree[][i])
{
fail[tree[][i]] = ;
q.push(tree[][i]);
}
}
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i = ; i < ; i++)
{
if(tree[now][i])
{
fail[tree[now][i]] = tree[fail[now]][i];
q.push(tree[now][i]);
}
else
{
tree[now][i] = tree[fail[now]][i];
}
}
} } void query(char *str)
{
int now = ;
for(int i = ; str[i] ; i++)
{
now = tree[now][str[i] - 'a'];
for(int j = now ; j && color[j] != - ; j = fail[j])
{
ans += color[j] ;
color[j] = - ;
}
} } int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
memset(tree, ,sizeof(tree));
memset(color , , sizeof(color));
ans = ;
k = ;
int n ;
scanf("%d" , &n);
for(int i = ; i < n ; i++)
{
scanf("%s" , a);
winsert(a);
}
fail[] = ;
getfail();
scanf("%s" , str);
query(str);
printf("%d\n" , ans);
} return ;
}
ac自动机(tree+kmp模板)的更多相关文章
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- AC自动机及KMP练习
好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...
- AC自动机——看似KMP在跑,其实fail在跳
先存代码 AC自动机(简单版) #include<bits/stdc++.h> #define maxn 1000007 using namespace std; int n,ans; i ...
- AC自动机(Aho-Corasick automation)模板 HDU:2222
#include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...
- HDU2222【AC自动机(基础·模板)】
Frist AC zi dong ji(Aho-Corasick Automation) of life #include <bits/stdc++.h> using namespace ...
- Trie树&kmp&AC自动机&后缀数组&Manacher
Trie 计数+Trie,读清题意很重要 https://vjudge.net/problem/UVALive-5913 kmp AC自动机 模板:https://vjudge.net/problem ...
- HDU:2222-Keywords Search(AC自动机模板,匹配模拟)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) P ...
- AC自动机--summer-work之我连模板题都做不出
这章对现在的我来说有点难,要是不写点东西,三天后怕是就一无所有了. 但写这个没有营养的blog的目的真的不是做题或提升,只是学习学习代码和理解一些概念. 现在对AC自动机的理解还十分浅薄,这里先贴上目 ...
- 【模板】AC自动机(简单版)
我:“woc...AC自动机?” 我:“可以自动AC???” 然鹅... 大佬:“傻...” 我:“(⊙_⊙)?” 大佬:“缺...” 我:“......” (大佬...卒 | 逃...) emm.. ...
随机推荐
- Python之路-Python常用模块-time模块
一.time模块 常用的一种获取当前时间以及时间格式化的模块,模块名称:time time模块在Python原生安装中就存在所以不需要进行任何安装操作,直接使用即可. 导入方式: import tim ...
- MongoDB的使用学习之(二)简介
原文链接:http://www.cnblogs.com/yxlblogs/p/3681089.html MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSQL 数据库产品中最热门 ...
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- Java使用多线程发送消息
在后台管理用户信息的时候,经常会用到批量发送提醒消息,首先想到的有: (1).循环发送列表,逐条发送.优点是:简单,如果发送列表很少,而且没有什么耗时的操作,是比较好的一种选择,缺点是:针对大批量的发 ...
- mysql 通过navicat 添加函数或者过程
1. 添加函数时, 函数参数的 varchar(255) 一定要加上 255,返回也要加.不加一直保存不了,狂试: 2. 添加过程时, 进入课程体 编辑时 也要加上 varchar 的位数限制.不 ...
- POJ 2960 S-Nim (sg函数)
题目链接:http://poj.org/problem?id=2960 题目大意:给定数组S,接下来给出m个游戏局面.游戏局面是一些beads堆,先给出堆数,然后是每一堆中beads的数目.游戏规则是 ...
- 对vueloader的研究
vue-loader是webpack的加载器,允许您以称为单文件组件(SFC)的格式创作Vue组件: <template> <div class="example" ...
- iOS设计模式之桥接模式
一,什么是桥接模式 定义 抽象出层次结构.上层抽象接口的职能,实现上层抽象接口的职能,层级间的通信协议(可以抽象为接口).桥接模式的目的,就是把抽象层次结构从具体的实现中分离出来,使其能够独立变更.抽 ...
- 【转】Django-template模板语言
Django-template模板语言 转自:https://www.cnblogs.com/zzy-9318/p/8672945.html 一.常用语法 只需要记两种特殊符号: {{ }}和 {% ...
- ASP 转换HTML特殊字符
Function HtmlDecode(ByVal s) If Has(s) Then s = regReplace(s, "<br\s*/?\s*>", vbCrLf ...