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/转 ...
随机推荐
- java 级联删除文件夹下的所有文件
public void deletefile(String delpath) throws Exception { try { File file = new File(delpath); // 当且 ...
- C# Cookie工具类
/// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键& ...
- Unity-The Courtyard demo学习
1.编辑器相机同步 The Courtyard的demo中,会发现Scene视图和Game视图是编辑器下同步的,它通过一个CopySceneView.cs脚本实现 Scene视图和Game视图的显示效 ...
- Codeforces Round #339 (Div.2)
A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- jquery之replaceAll(),replaceWith()方法详解
一:replaceAll() replaceAll()函数用于使用当前匹配元素替换掉所有的目标元素. 该函数属于jQuery对象(实例). 语法 jQuery 1.2 新增该函数. jQueryObj ...
- Java 中文字符串编码之GBK转UTF-8
写过两篇关于编码的文章了,以为自己比较了解编码了呢?! 结果今天又结结实实的上了一课. 以前转来转去解决的问题终归还是简单的情形.即iso-8859-1转utf-8,或者iso-8859-1转gbk, ...
- js插入动态脚本
原文章:https://www.w3cmm.com/dom/insert-javascript.html 动态脚本指的是在页面加载时不存在,但将来的某一时刻通过修改该DOM动态添加的脚本.和操作HTM ...
- LTE Module User Documentation(翻译9)——Using the EPC with emulation mode
LTE用户文档 (如有不当的地方,欢迎指正!) 15 Using the EPC with emulation mode(使用仿真方式的 EPC) 在上一节中,我们使用点对点链路连接基站和服务 ...
- postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed.
Destination: public.train_polylineSource File: C:\Documents and Settings\Administrator\桌面\ffffff\tra ...
- iOS - Xcode 常用快捷键
Xcode 常用快捷键 1)文件: command + shift + n 新建项目 command + n 新建文件 command + control + n 新建空文件 command + o ...