【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 ...
随机推荐
- selenium安装与更新
1.通过pip show selenium 查看是否已经安装过selenium,如果已经安装selenium会显示安装的selenium的版本信息. 如果在使用pip 查看命令报Unknown or ...
- 阿里前端实习生面试总结(两轮技术面+一轮hr面)
投的蚂蚁金服: 一面(只有13分钟): 1.angular里双向绑定的实现原理: 巴拉巴拉巴拉,这个问题很常见,我提到了$scope.$apply()和$scope.$digest(),面试官问app ...
- xpath 轴定位表达方式
xpath的使用基本语法: 1.// 从根节点开始,查找对象是全文. 2./ 从当前标签的路径开始查找 3.text()获取当前路径下的文本 4.@+类名或者id名 查找类名或者id的名字 5. .一 ...
- PHP魔术方法和魔法变量详解
一.魔术常量 __LINE__ 文件中的当前行号.__FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名. 自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对 ...
- insmod mknod
insmod module 會在 /proc/devices/ 下產生 major number 及 device name ---------------------------- mknod 會使 ...
- Activiti数据库支持
Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识. 用途也和服务的API对应. ACT_RE_*: 'RE'表示repository. 这个前缀 ...
- 监控查询慢sql
mysql:--查询慢sql:业务db用户 select b.time, b.host, b.id, b.state, b.user, b.db, b.info from information_s ...
- VBA 格式化excel数据表 (数据分列)
Sub ImportData() ' ' Copy Data from one workbook to the Current Workbook ' Place the macro file in t ...
- WPF 设置TextBox为空时,背景为文字提示。
<TextBox FontSize="> <TextBox.Resources> <VisualBrush x:Key="HelpBrush" ...
- 004-jdk-数据结构-ArrayList、LinkedList
一.ArrayList概述 数组集合,无容量限制,非线程安全 ArrayList.Vector是线性表,使用Object数组作为容器去存储数据的,添加了很多方法维护这个数组,使其容量可以动态增长,极大 ...