#include <iostream>
using namespace std; struct Node{
Node *next[];
Node* fail;
int count;
Node(){
for (int i = ; i < ; i++){
next[i] = NULL;
}
fail = NULL;
count = ;
}
};
char words[], s[];
Node* d[];
void insert(char words[], Node *root){
int i, len = strlen(words), v;
Node *p = root;
for (i = ; i < len; i++){
v = words[i] - 'a';
if (p->next[v] == NULL){
p->next[v] = new Node();
}
p = p->next[v];
}
p->count++;
}
void build(Node *root){
int head, tail, i;
Node *p, *temp;
head = ;
tail = ;
root->fail = NULL;
d[head] = root;
while (head <= tail){
temp = d[head++];
for (i = ; i < ; i++){
if (temp->next[i] == NULL) continue;
if (temp == root){
temp->next[i]->fail = root;
}
else{
p = temp->fail;
while (p != NULL){
if (p->next[i] != NULL){
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL){
temp->next[i]->fail = root;
}
}
d[++tail] = temp->next[i];
}
}
} int query(char s[], Node* root){
int ans = , len = strlen(s), i, v;
Node *p = root, *temp;
for (i = ; i < len; i++){
v = s[i] - 'a';
while (p->next[v] == NULL && p != root){
p = p->fail;
}
p = (p->next[v] != NULL) ? p->next[v] : root;
temp = p;
while (temp != root&&temp->count != -){
ans += temp->count;
temp->count = -;
temp = temp->fail;
}
}
return ans;
} int main(){
int m, n, i;
Node *root;
cin >> m;
while (m--){
root = new Node();
cin >> n;
for (i = ; i < n; i++){
cin >> words;
insert(words, root);
}
build(root);
cin >> s;
cout << query(s, root) << endl;
}
}

最新文章

  1. LA4287--tarjan
  2. SDL 1.2.15 issue
  3. 你用过这种奇葩的C#注释吗?如何看待
  4. Ogre分层渲染 (转)
  5. 关于mapreduce过程中出现的错误:Too many fetch-failures
  6. 编码、解码Html代码
  7. 机器学习-review-1 线性回归
  8. Win10使用小技巧
  9. TextWatcher原因activity内存泄漏问题
  10. React+Redux实现追书神器网页版
  11. MVC系列——一个异常消息传递引发的思考
  12. STL - stack(栈)
  13. luogu4643 [国家集训队]阿狸和桃子的游戏
  14. [Python数据挖掘]第2章、Python数据分析简介
  15. MySQL--字符集参数
  16. java基础知识学习--------之枚举类型(1)
  17. [原]C# 常用函数统计
  18. SPClaimsUtility.AuthenticateFormsUser的证书验证问题
  19. 全球第一开源云ERP Odoo操作手册 模块安装和界面汉化指南
  20. Socket编程 - 网络基础知识

热门文章

  1. Adobe CC update (Windows/Mac OS) 独立升级包下载
  2. caffe(9) caffe例子
  3. bzoj 2259: [Oibh]新型计算机 最短路 建模
  4. NodeJS学习笔记 进阶 (8)express+morgan实现日志记录(ok)
  5. NodeJS学习笔记 (28)流操作-stream(ok)
  6. Java 调用对象方法的执行过程
  7. d3 bubble源码分析
  8. python 调试大法-大笨蛋的笔记
  9. 题解 P2068 【统计和】
  10. Project Euler:Problem 58 Spiral primes