http://acm.hdu.edu.cn/showproblem.php?pid=2222

注意:

1. keyword可以相同,因此计算时要累计:cur->num++。

2. 同一个keyword在str中出现多次的话,只计算一次,所以算过后需要将num设为-1(表示已经计算了)。

对于

2
5
she
he
say
shr
her
yasherhs
5
she
he
say
sher
her
yasherhs

sher中包含:she,he,her,sher四个关键字,计算顺序:she->he->e->sher->her。

注意:匹配到一个关键字,需要继续把这个keyword中包含的其他的短的keyword都匹配了,再继续前移。

# include <cstdio>
#include <stdlib.h>
#include "memory.h"
#include "queue" const int MAX_N = 1000010; char s[MAX_N]; struct TNode {
int num;
TNode *suffix;
TNode *next[26];
TNode() {
clean();
}
void clean() {
num = 0;
suffix = NULL;
memset(next, NULL, sizeof next);
}
}; TNode *T[MAX_N]; int pos = 0; void buildTrie(char *str) {
TNode * cur = T[0];
for (int i = 0; str[i]; ++i) {
int c = str[i] - 'a';
if (cur->next[c] == NULL) {
if (T[++pos] == NULL) {
T[++pos] = new TNode();
}
cur->next[c] = T[pos];
}
cur = cur->next[c];
}
cur->num++;
} void addSuffix() {
TNode* root = T[0];
root->suffix = root;
std::queue<TNode*> qTrie;
for (int i = 0; i < 26; ++i) {
if (root->next[i] == NULL) {
root->next[i] = root;
} else {
root->next[i]->suffix = root;
qTrie.push(root->next[i]);
}
}
TNode *cur, *suf;
while (!qTrie.empty()) {
cur = qTrie.front(), qTrie.pop();
suf = cur->suffix;
for (int i = 0; i < 26; ++i) {
if (cur->next[i] == NULL) {
cur->next[i] = suf->next[i];
} else {
cur->next[i]->suffix = suf->next[i];
qTrie.push(cur->next[i]);
}
}
}
} int query(char *str) {
int ans = 0;
TNode * cur = T[0];
for (int i = 0; str[i]; ++i) {
int c = str[i] - 'a';
cur = cur->next[c];
TNode * back = cur; while (back != T[0] && back->num != -1) {
// printf("%c %d\n", str[i], back->num);
ans += back->num;
back->num = -1;
back = back->suffix;
}
}
return ans;
} void clean() {
for (int i = 0; i <= pos; ++i) {
if (T[i] != NULL) {
T[i]->clean();
}
}
pos = 0;
} int main() {
if (freopen("/Users/fripside/ClionProjects/cc150/input", "r", stdin) == NULL) {
fprintf(stderr, "error redirecting stdout\n");
}
T[0] = new TNode;
int c, t;
scanf("%d", &c);
while (c--) {
clean();
scanf("%d", &t);
while (t--) {
scanf("%s", s);
buildTrie(s);
}
addSuffix();
scanf("%s", s);
printf("%d\n", query(s));
}
return 0;
}

  

HDU2222的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

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

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

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

  3. HDU2222 Keywords Search 【AC自动机】

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

  4. 【AC自动机】hdu2222 Keywords Search

    AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...

  5. HDU-2222 Keywords Search 字符串问题 AC自动机

    题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...

  6. 【hdu2222】【poj2945】AC自动机入门题

    HDU2222 传送门 题目分析 裸题:注意构建自动机用的是模式串,思想和kmp很类似. code: #include<iostream> #include<cstdio> # ...

  7. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  8. hdu2222 Keywords Search ac自动机

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

  9. HDU2222 Keywords Search

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

随机推荐

  1. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  2. NodeJS的安装

    1. 进入官网下载对应版本的nodejs(我选择的是32位的window7版本x86) 2. 默认路径可修改   3. install就可以安装在cmd中输入path查看路径(我的是E:\compan ...

  3. HashCode

    如果一个类的对象要用做hashMap的key,那么一定要注意覆盖该类的equals和hashCode方法. equals()是基类Object的方法,用于判断对象是否有相同地址及是否为同一对象 pub ...

  4. ZIP等

    file命令可以查看文件的类型 tar类型 .targzip类型 .gz bzip2类型 .bz2zip类型 .zip 如果一个压缩文件由tar命令解压的前提,2个条件1.这个文件必须是由tar打包之 ...

  5. 浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真

    浅谈用ModelSim+Synplify+Quartus来实现Altera FPGA的仿真 工作内容: Mentor公司的ModelSim是业界最优秀的HDL语言仿真软件,它能提供友好的仿真环境,是业 ...

  6. HTML基础(2)

    常见标签 1.img标签 <img src="图片地址" alt="图片名"/>  图片(单标签) alt属性 是图片名字,是给百度搜索引擎抓取使用 ...

  7. Hibernate <查询缓存>

    查询缓存: 定义:查询缓存它是基于二级缓存的,可以保存普通属性查询的结果,查询对象实体时,他会保存id作为键,查询结果作为值,下个对象访问时,可以直接查到 查询缓存查询实体对象时,显著的特点是,会执行 ...

  8. python + selenium相关事件和元素定位

    女友由于工作上的失误,将公司RDM中的某一字段的2000条数据给删除了.....就算是重新添加字段,但是与其他数据的关联性已经不在了.由于每天的数据修改量大,有关部门不愿意恢复数据库,因此只能一条条的 ...

  9. html 通用 遮罩弹出层 弹出后 支持跳转页面

    //showMessage 提示的内容默认为空必填 buttonText:按钮显示的内容默认为"确定" 传入 "" 为默认 url:跳转链接 传入"& ...

  10. git: No refs in common and none specified; doing no

    用gitolite新建项目,clone后首次push,可能会出现:     $ git push No refs in common and none specified; doing nothing ...