【AC自动机】Keywords Search
【题目链接】
【题意】
原题来自:HDU 2222
给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文章,问:文中出现了多少个待查询的单词。多组数据。
【题解】
模板题
【代码】
#pragma GCC optimize(2) #include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 5e5+1e4;
const int M = 1e6+;
queue < int > Q ;
typedef struct Aho_Corasick_Automaton{
int Son[N][],End[N],Fail[N],idx;
void Init(){
idx = ;
while( !Q.empty() ) Q.pop() ;
memset(Son , , sizeof Son );
memset(End , , sizeof End );
memset(Fail, , sizeof Fail );
}
void Insert(char s[]){
int p = ;
for(int i=;s[i];i++){
int t = s[i] - 'a';
if( !Son[p][t] )
Son[p][t] = ++idx;
p = Son[p][t] ;
}
End[p] ++ ;
} void Build(){
for(int i=;i<;i++){
if( Son[][i] )
Fail[Son[][i]] = ,Q.push(Son[][i]);
} while( !Q.empty() ){
int u = Q.front() ;
Q.pop(); for(int i=;i<;i++){
if( Son[u][i] ){
Fail[Son[u][i]] = Son[Fail[u]][i] ;
Q.push( Son[u][i] );
}else{
Son[u][i] = Son[Fail[u]][i];
}
}
}
} void Query(char s[]){
int p = ,res = ;
for(int i=;s[i];i++){
int t = s[i] - 'a';
p = Son[p][t] ;
for(int j=p ; j && ~End[j] ; j = Fail[j] ){
res += End[j] ;
End[j] = - ;
}
}
//printf("%d\n",res);
cout << res << endl ;
}
}AC_Machine ;
AC_Machine AC ;
char s[M];
int n;
int main(){ ios_base :: sync_with_stdio(false);
cin.tie(NULL) , cout.tie(NULL);
int T;
cin >> T ;
while(T--){
cin >> n ;
AC.Init();
for(int i=;i<n;i++){
//scanf("%s",s);
cin >> s ;
AC.Insert(s);
}
AC.Build();
//scanf("%s",s);
cin >> s ;
AC.Query(s);
}
return ;
}
【AC自动机】Keywords Search的更多相关文章
- AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu----(2222)Keywords Search(ac自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- elasticsearch _bulk api
https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.htmlbulk API 允许在单个步骤中进行多次 create . ...
- elasticsearch sql插件配置(5.0及以上版本)
github官方参考地址:https://github.com/NLPchina/elasticsearch-sql/ 采用 git + node 的方式,所以安装前需要先安装好node,node n ...
- kotlin 代码格式
与java 不同,代码顺序影响编译过程. { var a:Int = 1; init(){ a++; } //var a:Int = 1; 写在 init 之后不可用. }
- LeetCode 被围绕的区域
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例: X X X X X O O X X X O X X ...
- strace调试跟踪程序运行状态
查看进程调用和执行状态 : strace -f -F -o debug.log -p PID(某个进程ID) 参考资料: http://www.itshouce.com.cn/linu ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_01-自定义查询页面-服务端-Dao
在页面输入查询条件,查询符合条件的页面信息. 查询条件如下: 站点Id:精确匹配 模板Id:精确匹配 页面别名:模糊匹配 spring mongoDB如何自定义条件 在Repository的findA ...
- 一百三十五:CMS系统之UEditoe编辑器集成以及配置将图片上传到七牛
富文本编辑框,选择UEditor 下载地址:http://ueditor.baidu.com/website/download.html 使用说明:http://fex.baidu.com/uedit ...
- Python升级提示Tkinter模块找不到的解决方法
一.安装tkinter在Linux中python默认是不安装Tkinter模块,复制代码 代码如下:[root@li250-193 ~]# pythonPython 2.6.6 (r266:84292 ...
- JKD1.8新特性
1.Optional类 Optional是jdk1.8引入的类型,Optional是一个容器对象,它包括了我们需要的对象,使用isPresent方法判断所包 含对象是否为空,isPresent方法返回 ...
- Jmeter综合运用 之 接口测试
Jmeter用的最多的就是做接口和性能测试了,着实比较好用,今天给大家分享下如何利用Jmeter做接口测试. 在做接口测试之前,我们起码需要了解: 1.接口涉及的业务 2.接口的基本信息:访问地址.传 ...