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.

InputFirst 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.

OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3

题意:

T组输入,对于每组数据有n个模式串,最后输入一个待求串。让你求这个待求串中有多少种模式串

注意:这个模式串可能会重复,重复的话要算做多个,不能算作一个

代码:

  1 /*
2 代码中:
3 叶节点:代表此节点下没有子节点
4 根节点:就是树的根
5 子节点:就是这个节点的直接相连的节点(直系节点)
6
7 此代码:
8 用题目所给模式串构成一颗字典树
9 然后找出来给出的待求串中有多少种模式串(如果模式串中有重复的,那么也算作多个,不算一个)
10
11 */
12 #include<stdio.h>
13 #include<iostream>
14 #include<string.h>
15 #include<algorithm>
16 #include<queue>
17 using namespace std;
18 const int maxn=5e5+10;
19 const int N=26;
20 typedef long long ll;
21 struct Trie
22 {
23 int next[maxn][N],fail[maxn],ends[maxn];
24 int root,L;
25 int New_node() //创建一个新节点
26 {
27 for(int i=0;i<N;++i)
28 {
29 next[L][i]=-1; //每次初始化新节点
30 }
31 ends[L++]=0; //每次初始化新节点
32 return L-1;
33 }
34 void init() //创建根节点
35 {
36 L=0;
37 root=New_node();
38 }
39 void inserts(char s[]) //往字典树里面插入新字符串
40 {
41 int len=strlen(s);
42 int now=root;
43 for(int i=0;i<len;++i)
44 {
45 if(next[now][s[i]-'a']==-1)
46 next[now][s[i]-'a']=New_node();
47 now=next[now][s[i]-'a'];
48 }
49 ends[now]++; //字典树的叶节点要标记,如果走到这个位置就代表这种模式串出现过
50 }
51 void build()
52 {
53 queue<int>r;
54 fail[root]=root;
55 for(int i=0;i<N;++i) //先给字典树根节点下的N个子节点初始化一下
56 {
57 if(next[root][i]==-1) //代表字典树上都没有i这个分支,对于这些节点的下一级就直接指向根节点就完了
58 {
59 next[root][i]=root;
60 }
61 else //字典树上有i这个分支的,就要改变它的fail(即,失败指针)的值
62 {
63 fail[next[root][i]]=root;
64 r.push(next[root][i]);
65 }
66 }
67 while(!r.empty())
68 {
69 int now=r.front();
70 r.pop();
71 for(int i=0;i<N;++i)
72 {
73 if(next[now][i]==-1)
74 {
75 next[now][i]=next[fail[now]][i];
76 //代表now在叶节点处,此时往下一个节点走就直接通过now节点失败指针进行跳转
77 }
78 else
79 {
80 fail[next[now][i]]=next[fail[now]][i]; //此节点的子节点的失败指针就是此节点失败指针对应字符位置
81 r.push(next[now][i]);
82 }
83 }
84 }
85 }
86 int query(char s[])
87 {
88 int len=strlen(s);
89 int now=root;
90 int res=0;
91 for(int i=0;i<len;++i)
92 {
93 now=next[now][s[i]-'a']; //这个now就保证了我们找的肯定是这个s字符串中的一部分
94 int temp=now; //这就是一直找,找到了就加1,找不到就通过失败指针看其他地方能不能找到
95 while(temp!=root) //尝试这个节点的失败指针能不能找到模式串
96 {
97 res+=ends[temp]; //因为我们要找出来s串中包含几种模式串,所以要有赋值0操作
98 ends[temp]=0;
99 temp=fail[temp];
100 }
101 }
102 return res;
103 }
104 };
105 char s[1000010];
106 Trie ac;
107 int main()
108 {
109 int t;
110 scanf("%d",&t);
111 while(t--)
112 {
113 int n;
114 scanf("%d",&n);
115 ac.init();
116 while(n--)
117 {
118 scanf("%s",s); //这个就是模式串
119 ac.inserts(s);
120 }
121 ac.build();
122 scanf("%s",s); //这个是待求串
123 printf("%d\n",ac.query(s));
124 }
125 return 0;
126 }

Keywords Search HDU - 2222 AC自动机板子题的更多相关文章

  1. hdu 2222(AC自动机模版题)

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

  2. HDU 2222 AC自动机模板题

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

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

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

  4. HDU 2222 AC自动机 裸题

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

  5. HDU 2222 AC自动机模版题

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

  6. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

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

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

  8. HDU 2896 (AC自动机模板题)

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

  9. HDU-2222 Keywords Search 字符串问题 AC自动机

    题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...

随机推荐

  1. kubernets之服务资源

    一  服务集群内部或者客户端与pod的通信桥梁   kubernets集群的内部pod访问为啥不能使用传统的IP:PORT的形式? pod是短暂的,它们会随时启动或者关闭,原因可能是pod所在的节点下 ...

  2. thinkpad8平板安装win10系统

    ThinkPad8 因为是平板电脑,只有一个micro USB接口,常规安装没法使用鼠标或键盘进行输入,所以难倒很多人. 幸好前段时间买了根otg线和3.0usb hub,安装方法记录如下: 准备:U ...

  3. 编年史:OI算法总结

    目录(按字典序) A --A* D --DFS找环 J --基环树 S --数位动规 --树形动规 T --Tarjan(e-DCC) --Tarjan(LCA) --Tarjan(SCC) --Ta ...

  4. uni-app开发经验分享二: uni-app生命周期记录

    应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...

  5. SUGA

    愿试炼的终点是花开万里 愿以渺小启程伟大结束 ----闵玧其

  6. linux编译模块,包含了头文件却还是报undifind警告

    在编写一个自己写的gadget驱动的时候遇到一个这样的问题,编译的时候报了个警告:WARNING: "usb_composite_register" [-/my_zero.ko] ...

  7. Less中Css预处理器

    Less.js 安装 npm install -g less 变量 basic 变量采用@进行变量定义.变量可以直接参加运算. @width:100px; .variables{ width:@wid ...

  8. SpringCloud zuul 网关限流分析

    最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml.pom配置 强烈推荐,按最新gith ...

  9. java虚拟机入门(二)-探索内存世界

    上节简单介绍了一下jvm的内存布局以及简单概念,那么对于虚拟机来说,它是怎么一步步的让我们能执行方法的呢: 1.首先,jvm启动时,跟个小领导一样会根据配置参数(没有配置的话jvm会有默认值)向大领导 ...

  10. Vue基础之插值表达式的另一种用法!附加变量的监听!

    Vue基础之插值表达式的另一种用法!附加变量的监听! 讲这个之前我们先回顾一下原来的用法! <body> <!-- Vue.js的应用可以分为两个重要的组成部分 一个是视图! 另一个 ...