HDU2222
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的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【AC自动机】hdu2222 Keywords Search
AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...
- HDU-2222 Keywords Search 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
- 【hdu2222】【poj2945】AC自动机入门题
HDU2222 传送门 题目分析 裸题:注意构建自动机用的是模式串,思想和kmp很类似. code: #include<iostream> #include<cstdio> # ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
随机推荐
- python:页面布局 后台管理页面之常用布局
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Sql Server2005新特性及性能
举几个例子来简单说明 这些例子我引用了Northwind库. 1. TOP 表达式 SQL Server 2000的TOP是个固定值,是不是觉得不爽,现在改进了. --前n名的订单declare @n ...
- Script循环语句 的相关知识跟练习
循环语句有两种问题类型:穷举和迭代 穷举: 在不知道什么情况下才是我们需要的结果的时候,只能让它一个一个的都执行一遍 迭代:在现有的条件下,根据规律,不断求解中间情况,最终推选出结果 两个关键词 br ...
- selenium高亮显示操作步骤方法
package com.allin.pc;import java.util.List;import org.openqa.selenium.WebElement;import org.openqa.s ...
- 使用XML定制Ribbon的一点小前奏(稍微再进一步的理解XML)
定制文档级Ribbon界面的实现思路: 1.excel的文件使用rar+xml的形式保存在本地. 2.用压缩软件打开文件,以规范的格式直接编缉或添加xml文件 3.使用excel文件时,主程序会解析x ...
- gdb调试正执行的程序
参考,转载:http://biancheng.dnbcw.info/linux/391846.html
- php : 类常量
使用总结: 1.不能使用 define 来定义 2.通过 "类名::常量名" 来获取 /** * PHP类常量 * * 类常量属于类自身,不属于对象实例,不能通过对象实例访问 * ...
- Canvas学习
参考了慕课网课程:炫丽的倒计时效果Canvas绘图与动画基础 感谢 liuyubobobo 老师 ,提供了这么好的课程 1.<canvas><canvas>标签 注 ...
- Android 连接webservice(利用谷歌提供的jar包)
Android开发,需要连接webservice,之前就想用谷歌提供的jar包,下载地址:http://pan.baidu.com/s/1hqMTUHe 把它下载下来粘贴到libs文件夹下即可: 网上 ...
- windows下的Nodejs及npm的安装、常用命令,Nodejs开发环境配置
http://www.cnblogs.com/webstorm/p/5744942.html ***************************************** 第一步:下载Nodej ...