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. vs2010下编译osip2和eXosip2的4.0.0版的静态库及搭建开发环境

    转载于:http://blog.csdn.net/lbc2100/article/details/48293911 本文为参考网上的动态库的方式,进行静态库的实现,在此对动态库的作者表示感谢. 第一步 ...

  2. [转]SVN客户端解决authorization failed问题

    转载地址:http://blog.csdn.net/patdz/article/details/7669591 1. 创建文件夹 E:\STWSource\STWLibrarySVN 2.在文件夹ST ...

  3. Xcode环境下OpenGL C++ GLFW开发环境搭建

    操作系统版本:MacOS Mavericks 10.9.2 Xcode版本:5.0.1 OpenGL版本:4.10 GLFW版本:3.0.4 一.GLFW编译 1.下载并安装cmake(当前最新版本为 ...

  4. 替换 data.frame 中的特殊的值

    替换空值: foo <- data.frame("day"= c(1, 3, 5, 7), "od" = c(0.1, "#N/A", ...

  5. mongodb中的副本集搭建实践

    准备运行1个主节点,2个从节点,从节点中其中是一个是仲裁节点(Arb). --oplogSize --oplogSize --oplogSize 其中application是副本集的名称,节点必须相同 ...

  6. 解决lScrollView嵌套ListView只显示一行的问题,listvie显示全部的item

    ScrollView嵌套ListView只显示一行的问题 1.思路:给listview重新添加一个高度. listview的高度==listview.item的高度之和. 2.注意:关键是添加list ...

  7. [Oracle] SQL*Loader 详细使用教程(2)- 命令行参数

    sqlldr工具   SQL*Loader的客户端工具是sqlldr,在操作系统的命令行下输入sqlldr,后面不接任何参数,将显示帮助信息如下所示(所有命令行参数的简单描述及其默认值),所以你并不需 ...

  8. 【bzoj1084】最大子矩阵

    题意 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. \(1≤n≤100,1≤m≤2,1≤k≤10\) 分析 由于\(m\)只有两 ...

  9. linux 真·随笔

    复制操作 命令行模式下输入 6,9 co 12 复制第6行到第9行之间的内容到第12行后面. vim如何删除文件中所有东西 ggdG :%d 移动光标到指定行的行尾 $:移动光标到行尾 n$:移动到第 ...

  10. PetShop的系统架构设计

    <解剖PetShop>系列 一.PetShop的系统架构设计 http://www.cnblogs.com/wayfarer/archive/2007/03/23/375382.html ...