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. NOI1994 删数问题

    [问题描述]键盘输入一个高精度的正整数n(≤240位),去掉其中任意s个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的n和s,寻找一种方案,使得剩下的数字组成的新数最小. 输入 两行第 ...

  2. 2021版idea关于translation插件翻译报错

    cn.yiiguxing.plugin.translate.trans.TranslateException: Google 翻译 :: 翻译失败: 未知错误 这是idea拉下来的报错信息,trans ...

  3. STM32中断调试中遇到的问题

    STM32应用过程中遇到的问题 实现功能: 1.自动流水灯:在LED1~LED4上实现自动流水灯,流水间隔时间为200ms/bit,然后通过按键KEY1改变流水灯的速度,每次按键间隔时间增加200ms ...

  4. mysq 报错, sql语句在数据库里运行正常, 在内网测试正常,打包放外网的时候就报下面错误

    sql语句为: select t1.day as day , any_value(IFNULL(t2.avgNum,0)) as avgNum, any_value(IFNULL(t2.maxNum, ...

  5. Python基础数据类型-list(列表)

    a = [1, 2, 3] a.append([1, 4]) a.append(8) a.append([8, 9, '10']) # 添加的是整体,即:将输入对象添加到列表末尾 print(a) # ...

  6. PostgreSQL 存储过程 通过设定条件,返回指定的数据表记录

    PL/pgSQL是 PostgreSQL 数据库系统的一个可装载的过程语言. PL/pgSQL的设计目标是创建一种可装载的过程语言,可以可用于创建函数和触发器过程, 在SQL语言中添加控制结构功能, ...

  7. css/js使用小技巧记录

    1.白底小图标换色 .iconBox { position: relative; width: 19px; height: 19px; overflow: hidden; // 隐藏原本颜色的图片 . ...

  8. 操作系统|02.Linux基础(1)

    Linux基础 1.Linux系统安装.密码的破解 1.1常见的系统 unix:性能稳定,价格高昂,命令与Linux相通.多为大型政府单位.大型企业.金融机构使用. Linux:开源.自由 Linux ...

  9. svn批量的添加ignore

    svn没有批量添加ignore file的功能,只能一个个目录去设置,故写了脚本自动化的执行 使用了svn propset,故改了ignore file,可以反复的执行此脚本 #!/usr/bin/p ...

  10. c++ read and save txt

    read and save #include "util/image_util.h" #ifdef USE_PANGOLIN_VIEWER #include "pango ...