题目链接:https://cn.vjudge.net/problem/HDU-2222

题意

给一些关键词,和一个待查询的字符串

问这个字符串里包含多少种关键词

思路

AC自动机模版题咯

注意一般情况不需要修改build方法,就像kmp里的getfail一样

一般的题目就是改改insert,query

一开始写的模版总是有问题,懒得改了

直接找的kuangbin的模版【原创】AC自动机小结

注意数组和指针的效率差不了多少,此题同一个算法的指针形式(296ms)比数组(187ms)慢110ms

说实在的,数组写法就是优雅。

网上那些指针的看着就难受,明明是好好的逻辑,变量名都是pqrvtxy,搞的跟被混淆了一样。

改了两套没一个舒服的,真难受。

提交过程

AC

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int ACSize=500000+20;
const int maxitem=26, maxn=1e4+20, maxword=50+20, maxl=1e6+20;
char word[maxword], line[maxl];
struct Trie{
int next[ACSize][26], fail[ACSize], cnt[ACSize];
int root, total;
int newnode(void){
for(int pos=0; pos<maxitem; pos++)
next[total][pos]=-1;
cnt[total]=0;
return total++;
} void init(){
total=0;
root=newnode();
} void insert(char buf[]){
int now=root;
for(int i=0; buf[i]; i++){
int pos=buf[i]-'a';
if(next[now][pos]==-1)
next[now][pos]=newnode();
now=next[now][pos];
}
cnt[now]++;
} void build(void){
queue<int> que;
fail[root]=root;
for(int i=0; i<maxitem; i++)
if(next[root][i]==-1)
next[root][i]=root;
else{
fail[next[root][i]]=root;
que.push(next[root][i]);
} while(!que.empty()){
int now=que.front(); que.pop(); for(int pos=0; pos<maxitem; pos++)
if(next[now][pos]==-1)
next[now][pos]=next[fail[now]][pos];
else{
fail[next[now][pos]]=next[fail[now]][pos];
que.push(next[now][pos]);
}
}
} int query(char buf[]){
int now=root, res=0;
for(int i=0; buf[i]; i++){
int pos=buf[i]-'a';
now=next[now][pos];
for (int tmp=now; tmp!=root && cnt[tmp]!=-1; tmp=fail[tmp]){
res+=cnt[tmp];
cnt[tmp]=-1; // 注意此处,找到了就不要找第二次了,直接删除即可
}
}return res;
} void debug(void){
for(int i=0; i<total; i++){
printf("id=%3d, fail=%3d, end=%3d [", i, fail[i], cnt[i]);
for(int j=0; j<maxitem; j++)
printf("%2d", next[i][j]);
printf("]\n");
}
}
}AC; int main(void){
int T, n; scanf("%d", &T);
while (T--){
AC.init();
scanf("%d", &n);
for (int i=0; i<n; i++){
scanf("%s", word);
AC.insert(word);
}AC.build(); scanf("%s", line);
printf("%d\n", AC.query(line));
} return 0;
}
Time Memory Length Lang Submitted
187ms 27744kB 2369 G++ 2018-08-02 16:16:21

HDU-2222 Keywords Search 字符串问题 AC自动机的更多相关文章

  1. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  2. HDU 2222 Keywords Search(AC自动机模版题)

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

  3. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  4. HDU 2222 Keywords Search 【AC自动机】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=2222] 题意:给出很多小字符串,然后给出一个文本串,问文本串中包含多少个小字符串.也就是说如果文本串 ...

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

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

  6. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  7. HDU 2222 Keywords Search (AC自动机)

    题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

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

    题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...

随机推荐

  1. Java之Object类

    0 引言 Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类.Object类是Java中唯一没有父类的类. 其他所有的类,包括标准容器类,比如数组,都继承了Object类中的方法 ...

  2. js单体内置对象

    js单体内置对象:js的内置对象,是ECMAScritp提供的.不依赖于宿主环境的对象,我的理解就是在我们开发之前js里面就已经存在的对象.单体内置对象就是是不需要通过new来实例化的,例如我们的st ...

  3. ThoughtWorks 技术雷达(2013年5月)

    ThoughtWorks技术雷达(2013年5月) 作者ThoughtWorks技术战略委员会 发布于 六月 25, 2013| 讨论 新浪微博腾讯微博 豆瓣网 Twitter Facebook li ...

  4. [读书笔记] Python数据分析 (一) 准备工作

    1. python中数据结构:矩阵,数组,数据框,通过关键列相互联系的多个表(SQL主键,外键),时间序列 2. python 解释型语言,程序员时间和CPU时间衡量,高频交易系统 3. 全局解释器锁 ...

  5. 流媒体应用程序Mobdro或存在安全隐患

    Mobdro是一款流媒体应用程序,可以安装在任何Android设备上,包括手机,平板电脑,亚马逊的Fire TV Stick和Google的Chromecast.它现在已经流行了一段时间,特别是在围绕 ...

  6. 小学生都能学会的python(文件操作)

    小学生都能学会的python(文件操作) 1. open("文件路径", mode="模式", encoding="编码") 文件的路径: ...

  7. Mysql学习总结(32)——MySQL分页技术详解

    1.什么是数据分页:数据分页就是将很多条记录像书本一样分页,每页显示多少行记录: 2.为什么要数据分页:当我们进行sql语句查询时,假如数据有成千上万行记录,如果在同一个页面去显示,那这个页面得有多大 ...

  8. POJ 1715

    同样是确定某位上的数,当确定某一位后,其后面的排列数是确定的,所以可以用除法和取余数的方法来确定这一位的值 #include <iostream> #include <cstdio& ...

  9. Executors线程池关闭时间计算

    Executors线程池关闭时间计算 学习了:http://blog.csdn.net/wo541075754/article/details/51564359 https://www.cnblogs ...

  10. [CSS3] The different of Background-size between 'cover' and 'contain'

    'cover': The smaller axies of image (x axies) should match smaller axies (x axies) of container. So ...