Keywords Search  HDU - 2222

贴个vj的链接https://vjudge.net/problem/HDU-2222

题意:T组数据,n个单词,再给你一个串,看有几个单词在这个串里面出现过

Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std; const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; const int N = 5e5 + 1000;
int fa[N], trie[N][30], tot, vis[N]; void build(string s){
int len = s.size();
int pos = 0;
for(int i = 0; i < len; i++){
int son = s[i] - 'a';
if(!trie[pos][son]) trie[pos][son] = ++tot;
pos = trie[pos][son];
}
vis[pos]++;
}
void getfail(){
queue<int> q;
for(int i = 0, p; i < 26; i++){
p = trie[0][i];
if(p){
fa[p] = 0;
q.push(p);
}
}
while(q.size()){
int now = q.front();
q.pop();
for(int i = 0; i < 26; i++){
int u = trie[now][i];
if(u){
q.push(u);
fa[u] = trie[fa[now]][i];
}
else trie[now][i] = trie[fa[now]][i];
}
}
} int getans(string s){
int now = 0, ans = 0;
for(int i = 0; i < s.size(); i++){
now = trie[now][s[i]-'a'];
for(int j = now; j && vis[j]!=-1; j = fa[j]){
ans+=vis[j];
vis[j] = -1;
}
}
return ans;
} int main(){
int t; cin >> t;
fa[0] = 0;
while(t--){
memset(trie, 0, sizeof(trie));
memset(vis, 0, sizeof(vis));
tot = 0;
int n; cin >> n;
string s;
for(int i = 1; i <= n; i++){
cin >> s;
build(s);
}
getfail();
cin >> s;
cout << getans(s) << endl;
}
}

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

  1. HDU 2222 AC自动机模板题

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

  2. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

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

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

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

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

  5. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  6. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  7. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

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

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  9. HDU 2222 Keywords Search(AC自动机模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  10. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

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

随机推荐

  1. 在Unity3D中开发的Ghost Shader

    SwordMaster Ghost Shader 特点 此Shader是顶点片元Shader,由本人手动编写完成 此Shader已经在移动设备真机上进行过测试,可以直接应用到您的项目中 所支持的Uni ...

  2. NOI模板复习组——图论部分

    1.最小生成树: kruscal: #include <cstdio> #include <cmath> #include <cstring> #include & ...

  3. 标准c++中string类函数介绍

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是 ...

  4. b站——沐神——深度学习

    预备知识 数据操作 MXNet nd:(array函数:得到NDArray) [[1. 1. 1.] [1. 1. 1.]] <NDArray 2x3 @cpu(0)> np:(asnum ...

  5. git合并指定分支的commit到主干上

    1.先切换分支到master git checkout master 2.目前正处于主干上,执行命令,将分支从最后一个commit合并到主干上 git rebase --onto cb4023015f ...

  6. SpringBoot加载相关注解

    springBoot加载 @Configuration 表明该类是一个配置类常常配合@Bean使用,让容器管理对象 @Configuration(proxyBeanMethods = true) pr ...

  7. UI自动化之【chromedriver.exe无法删除问题】

    想删掉chromedriver.exe,结果提示被打开 在任务管理器中,找到Chromedriver.exe,结束进程

  8. NOI 顺序查找——查找特定的值

    描述 在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置. 输入 第一行包含一个正整数n,表示序列中元素个数.1 <= n <= 10000.第二行包含n个整数,依次给出序 ...

  9. [Leetcode 108]有序数组转BST二叉搜索树Convert Sorted Array to Binary Search Tree

    题目 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Given an integer array  ...

  10. Java数据类型基础

    Java 数据类型基础 数据类型 强类型语言 要求变量的使用要严格符合规定,所有变量必须先定义后使用 Java数据分为两大类 基本类型(primitive type) 数值类型 整数类型 byte(1 ...