HDU-2222 Keywords Search 字符串问题 AC自动机
题目链接: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自动机的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search 【AC自动机】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=2222] 题意:给出很多小字符串,然后给出一个文本串,问文本串中包含多少个小字符串.也就是说如果文本串 ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU 2222 Keywords Search(AC自动机)题解
题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...
随机推荐
- swift使用查阅资料备份1
SnapKit RxSwift R.swift https://www.jianshu.com/p/68e12b966d86 iOS - RxSwift 项目实战记录 https://blog.csd ...
- java 文件下载遇到的数个坑
文件的下载在web开发中应该是很常用的功能,近期项目中遇到的一个需求是:前端提供 查询条件以及查询结果的字段,后端拿到这些参数之后,在数据库中根据业务逻辑查询得出查询结果,导出成excel文件,同时传 ...
- hibernate---crateria
Leslie 趁还没忘掉,赶快记录下来 Hibernate中Criteria的完整用法 转自:http://www.360doc.com/content/090313/10/26262_2794855 ...
- Pyhton学习——Day9
#文件操作的其他方法 ###############################################################################文件的内置操作函数# ...
- vscode快捷键补充
ctrl+enter 快速还到下行并插入一行 ctrl+shift+enter 快速换到上行并插入一行 ctrl+~ ctrl+1 快速在终端与代码区切换 ctrl+i 选中一行 ctrl + p: ...
- Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...
- linux下wps的字体缺失解决方法
可以参考Mr.Liang 说明:当安装好wps for linux,然后打开wps会提示字体缺失,可做如下操作: 1.下载wps缺失字体(资源侵权联系我删除) 2.解压 unzip -d ./wps_ ...
- 11g,12c Oracle Rac安装
安装 Oracle 12cR1 RAC on Linux 7 本文介绍如何在Oracle Linux 7上安装2节点Oracle 12cR1 Real Application Cluster(RAC) ...
- angular-事件
ng-click事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <div ng-app="myApp ...
- [CSS3] All abourt responsive image
Take few examples: Full size image: The problem for that is it overflow when the screen size is smal ...