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和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...
随机推荐
- day19-2 生成器,递归函数
目录 生成器 有关yield的理解 实现range()函数 生成器表达式 递归 思考 斐波那契额 汉诺塔 二分法 生成器 自定义的迭代器 yield关键字: 和return一样,接收值,但不终止函数 ...
- 用 Java 技术创建 RESTful Web (服务 JAX-RS:一种更为简单、可移植性更好的替代方式)
作者: Dustin Amrhein, 软件工程师, IBM Nick Gallardo, 软件工程师, IBM 出处: http://www.ibm.com/developerworks/cn/we ...
- python学习笔记:第三天
day03: 1.python中所有的变量都可以改变 2.Print(name) 打印 3.Del name 删除 4.python中python2与python3不能兼容,需要分别安装pyth ...
- crm 系统项目(二) admin 后台操作表格
crm 系统项目(二) admin 后台操作表格 1. app下创建 templates 运行的时候 先找全局的templates——> 按照app的注册顺序找templates中的文件 2. ...
- 如何使用JAVA请求HTTP
package com.st.test; import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...
- Java并发和多线程1:并发框架基本示例
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括ThreadPool,Executor,Executors,ExecutorService,Com ...
- 引入拦截器及swagger支持及解决redis无法初始化问题
Springboot引入拦截器 自定义的拦截器类 Interceptor package cn.zytao.taosir.auth.config; import javax.annotation.Re ...
- Java线程:CountDownLatch 与Thread 的 join()
需求: 主程序中需要等待所有子线程完成后 再继续任务 两种实现方式: 一种使用join() 方法:当在当前线程中调用某个线程 thread 的 join() 方法时,当前线程就会阻塞,直到thread ...
- jquery-layer.closeAll不执行的错觉
在使用ajax.form提交的时候,弹出了layer插件的页面,于是我想使用layer插件提供的layer.closeAll()方法讲这个弹出的页面关闭,但是尝试了很久不行,到底是为什么呢? 过了一段 ...
- mycat的配置和学习
1.mycat一共三个配置文件. 1)server.xml:配置逻辑库的名称访问的账号密码 <user name="root"> <property name=& ...