【题目链接】

https://loj.ac/problem/10057

【题意】

原题来自: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的更多相关文章

  1. AC自动机---Keywords Search

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...

  2. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  3. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  4. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

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

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

  6. HDU2222 Keywords Search [AC自动机模板]

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

  7. hdu----(2222)Keywords Search(ac自动机)

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

  8. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  9. HDU2222 Keywords Search(AC自动机)

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

  10. hdu2222 Keywords Search【AC自动机】

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

随机推荐

  1. Java 使用线程池分批插入或者更新数据

    需求:在开发业务报表时,需要从MySQL数据库读取数据后进行操作,然后写入数据库,使用定时任务跑批. 分析:①兼顾性能,②  MySQL没有Oracle那么方便.强大的存储过程.综上所述,使用线程池以 ...

  2. react-native 打包apk 更新js和常见问题

    1.打包发布 a.在工程根目录执行如下命令 打包js资源文件 react-native bundle --platform android --dev false --entry-file index ...

  3. deepin安装下载,部署在虚拟机上

    第一次接触linux就用了很久的Ubuntu,后来尝试了Manjaro.Debian,还是用了Ubuntu,但是用VM虚拟机使用的ubuntu经常卡顿.我始终觉得ubuntu的默认桌面环境gnome可 ...

  4. easyui的combobox模糊搜索

    <tr> <th>测试名称:</th> <td> <select data-options="" class="ea ...

  5. K-Means算法及代码实现

    1.K-Means算法 K-Means算法,也被称为K-平均或K-均值算法,是一种广泛使用的聚类算法.K-Means算法是聚焦于相似的无监督的算法,以距离作为数据对象间相似性度量的标准,即数据对象间的 ...

  6. linux下如何设置日志级别?

    答: echo $level > /proc/sys/kernel/printk 如: echo 8 4 1 7 > /proc/sys/kernel/printk

  7. 断句:Store all parameters but the first passed to this function as an array

    // Store all parameters but the first passed to this function as an array //除了第一个参数,把调用publish函数时的所有 ...

  8. OpenStack v.s. Kubernetes

    目录 文章目录 目录 What are the differences with OpenStack and Kubernetes? Why OpenStack & Kubernetes? W ...

  9. scikit-learn机器学习(三)多项式回归(二阶,三阶,九阶)

    我们仍然使用披萨直径的价格的数据 import matplotlib matplotlib.rcParams['font.sans-serif']=[u'simHei'] matplotlib.rcP ...

  10. JAVA 基础编程练习题33 【程序 33 杨辉三角】

    33 [程序 33 杨辉三角] 题目:打印出杨辉三角形(要求打印出 10 行如下图) 程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 package ...