【题目链接】

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 ;
}

最新文章

  1. 跟着老男孩教育学Python开发【第四篇】:模块
  2. 开源跨平台IOT通讯框架ServerSuperIO,集成到NuGet程序包管理器,以及Demo使用说明
  3. Canvas电子签名和游戏化
  4. iOS开发网络请求——大文件的多线程断点下载
  5. fork()函数详解
  6. [Effective JavaScript 笔记]第33条:使构造函数与new操作符无关
  7. Arrays.asList引起的惨案
  8. Java多线程(五) Lock接口,ReentranctLock,ReentrantReadWriteLock
  9. PSR-4——新鲜出炉的PHP规范
  10. Android游戏快速入门(一):基础储备
  11. OpenGL 4 : 一个漂亮的心 For you, My Love
  12. executeBatch()相关操作汇总
  13. FastJson基本使用
  14. js的学习(window对象的使用)
  15. 组件嵌套时报:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
  16. OpenJudge-bailian 3454 秦腾与教学评估
  17. group by分组后获得每组中符合条件的那条记录
  18. 程序员常用字体(vs2008字体修改方案)
  19. 剑指offer例题——二进制中1的个数
  20. pyqt5 添加属性-类方法用属性形式访问

热门文章

  1. HDU 1069 Monkey and Banana ——(DP)
  2. easyui-combobox和C标签判断回显
  3. vue实战_从头开始搭建vue工程
  4. 理解了这些异常现象才敢说真正懂了TCP协议
  5. ICEM—奇葩
  6. WIN10环境下点击通知栏图标时自动切换输入法导致图标位置变动
  7. windows工程总结
  8. 认识理解Java中native方法(本地方法)
  9. Centos 6.8环境下OpenLDAP安装与部署
  10. [!] The version of CocoaPods used to generate the lockfile (1.4.0.beta.1) is higher than the version of the current executable (1.3.0.beta.1). Incompatibility issues may arise.