Keywords Search

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

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
 
Sample Output
3
没什么好说的:上模板
https://blog.csdn.net/bestsort/article/details/82947639
https://www.cnblogs.com/cjyyb/p/7196308.html
#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模板)的更多相关文章

  1. 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...

  2. AC自动机及KMP练习

    好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...

  3. AC自动机——看似KMP在跑,其实fail在跳

    先存代码 AC自动机(简单版) #include<bits/stdc++.h> #define maxn 1000007 using namespace std; int n,ans; i ...

  4. AC自动机(Aho-Corasick automation)模板 HDU:2222

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...

  5. HDU2222【AC自动机(基础·模板)】

    Frist AC zi dong ji(Aho-Corasick Automation) of life #include <bits/stdc++.h> using namespace ...

  6. Trie树&kmp&AC自动机&后缀数组&Manacher

    Trie 计数+Trie,读清题意很重要 https://vjudge.net/problem/UVALive-5913 kmp AC自动机 模板:https://vjudge.net/problem ...

  7. HDU:2222-Keywords Search(AC自动机模板,匹配模拟)

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

  8. AC自动机--summer-work之我连模板题都做不出

    这章对现在的我来说有点难,要是不写点东西,三天后怕是就一无所有了. 但写这个没有营养的blog的目的真的不是做题或提升,只是学习学习代码和理解一些概念. 现在对AC自动机的理解还十分浅薄,这里先贴上目 ...

  9. 【模板】AC自动机(简单版)

    我:“woc...AC自动机?” 我:“可以自动AC???” 然鹅... 大佬:“傻...” 我:“(⊙_⊙)?” 大佬:“缺...” 我:“......” (大佬...卒 | 逃...) emm.. ...

随机推荐

  1. adb 链接网络 connect 安装apk install 断开IP链接 kill-server 连接数devices

    https://blog.csdn.net/zhonglunshun/article/details/78362439 ./adb connetc 192.168.1.11 ./adb install ...

  2. Linux基础之输入输出

    第十五章 输入输出 一.重定向概述 1.1.什么是重定向? 将原本要输出到屏幕的数据信息,重新定向到指定的文件中. 比如:每天凌晨定时备份数据,希望将备份数据的结果保存到某个文件中.这样第二天通过查看 ...

  3. Promise.all 的原理

    // all的原理 Promise.all = function(values){ return new Promise((resolve,reject)=>{ let results = [] ...

  4. Oracle12c修改时区

    Oacle12c支持可插入数据库(PDB)在一个统一的数据库(CDB)中具有不同的字符集.时区文件版本和数据库时区. 出于性能原因,Oracle建议将数据库时区设置为UTC(0:00),因为不需要转换 ...

  5. POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )

    题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A  B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...

  6. 用树状数组写的最长上升子序列(友好城市),nlogn。

    #include<iostream> #include<algorithm> #define maxn 100000 #define lb(x) x&-x using ...

  7. Cluster基础(三):配置HAProxy负载平衡集群、Keepalived高可用服务器、Keepalived+LVS服务器

    一.配置HAProxy负载平衡集群 目标: 准备三台Linux服务器,两台做Web服务器,一台安装HAProxy,实现如下功能: 客户端访问HAProxy,HAProxy分发请求到后端Real Ser ...

  8. 《数据结构与算法(C语言版)》严蔚敏 | 第四章课本案例

    //二叉树的顺序存储表示 #define MAXTSIZE 100 typedef TElemtype SqBiTree[MAXTSIZE]; SqBiTree bt; //二叉树的二叉链表存储表示 ...

  9. [CSP-S模拟测试]:飞(fly)(数状数组+简单几何)

    题目描述 $liu\_runda$决定提高一下知识水平,于是他去请教郭神.郭神随手就给了$liu\_runda$一道神题,$liu\_runda$并不会做,于是把这个题扔到联考里给高二的做.郭神有$n ...

  10. Hibernate入门学习笔记

    1.Hibernate是什么? 2.hibernate怎么配置? 3.SessionFactory是干什么的?有哪些方法经常用? 4.hibernate的现成的增删改查方法怎么使用?都有哪些方法?哪些 ...