ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search
Description
Input
First line will contain one integer means how many cases will follow by. Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000) Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50. The last line is the description, and the length will be not longer than 1000000.
Output
Sample Input
Sample Output
但是这个题目需要注意两点(被坑的两点): 1、关键词中出现的重复单词算多个,而且最好在查找的时候,找到一个,其它的都算被找到了。 2、最后查找的时候重复的只算一次。
28M、700MS过的。最好需要释放一下动态内存。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 0x3fffffff using namespace std; struct tree
{
tree *next[26];
int End;
bool Endvis;
}; char s[1000005]; void Add(char *str, tree *head)
{
char *a = str;
tree *p = head;
while (a[0] != '\0')
{
int k = a[0]-'a';
if (p->next[k] == NULL)
{
tree *p1;
p1 = (tree*)malloc(sizeof(tree));
for (int i = 0; i < 26; ++i)
{
p1->next[i] = NULL;
}
p1->End = 0;
p1->Endvis = 0;
p->next[k] = p1;
}
p = p->next[k];
++a;
}
p->End += 1;
} int Find (char *str, tree *head)
{
char *s = str;
tree *p = head;
int ans = 0;
while (s[0] != '\0')
{
int k = s[0]-'a';
if (p->next[k] == NULL)
{
break;
}
else
{
p = p->next[k];
}
if (p->End > 0 && p != head)
{
if (p->Endvis == 0)
{
ans += p->End;
p->Endvis = 1;
}
}
++s;
}
if (p == head) return 0;
return ans;
} int main()
{
//freopen ("test.txt", "r", stdin);
int T;
scanf ("%d", &T);
for (int times = 0; times < T; ++times)
{
tree *head;
int n;
head = (tree*)malloc(sizeof(tree));
for (int i = 0; i < 26; ++i)
{
head->next[i] = NULL;
}
scanf ("%d", &n);
for (int i = 0; i < n; ++i)
{
char str[55];
scanf ("%s", str);
if (str[0] != '\0') Add (str, head);
}
scanf ("%s", s);
int len = strlen(s), ans = 0;
for (int i = 0; i < len; ++i)
{
ans += Find(s+i, head);
}
printf ("%d\n", ans);
}
return 0;
}
ACM学习历程—HDU2222 Keywords Search(字典树)的更多相关文章
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
随机推荐
- 栈(C++)
简介: 限定仅在表尾进行插入或删除操作的线性表 表尾端称为栈顶(top),表头端称为栈底(bottom) 特点: 在栈中,后入栈的元素先出栈 C语言版本 用于测试的文件,以及测试结果可以去作者GitH ...
- caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...
- 使用HTML5制作简单的RPG游戏
很久以前就想着做一个游戏,但什么都不会又不知道从哪里开始,胡乱找来一些书籍和资料结果太深奥看不懂,无奈只能放弃.这一弃就是十多年,倥偬半生,眼看垂垂老矣,还是没能有什么成果. 近年来游戏引擎越来越多, ...
- 记一次Oracle数据故障排除过程
前天在Oracle生产环境中,自己的存储过程运行时间超过1小时,怀疑是其他job运行时间过长推迟了自己job运行时间,遂重新跑job,发现同测试环境的确不同,运行了25分钟. 之后准备在测试环境中制造 ...
- 使用Erlang和Thrift,与Hbase通信(转)
操作系统是Ubuntu Server 12.10 先安装Thrift sudo apt-get install libboost-dev libboost-test-dev \ libboost-pr ...
- 初探swift语言的学习笔记四-2(对上一节有些遗留进行处理)
作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/30314359 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...
- 对你的 REST API 进行保护的正确办法
设计好一个美丽的 REST + JSON API 之后,怎样对你的 API 进行保护?在 Stormpath,我们花了 18 个月来寻找最佳实践.将其一一实践于 Stormpath API 中并分析其 ...
- $CLASS('page__hd')[0].style.backgroundColor="red"
const $ID = (p) => document.getElementById(p)const $CLASS = (p) => document.getElementsByClass ...
- Swift 学习笔记 (枚举)
枚举为一种相关值定义了一个通用类型,从而可以让你在代码中类型安全的操作这些值. Swift中的枚举很灵活,不需要给每一个枚举中的成员都提供值.如果一个值(所谓 原时值) 要被提供给每一个枚举成员,那么 ...
- 基于 HTTP 协议的几种实时数据获取技术(转)
HTTP协议 HTTP协议大家都很熟悉了,开始本文之前,首先简单回顾一下HTTP协议. HTTP协议是建立在TCP协议上的应用层协议,协议的本质是请求----应答: 即对于HTTP协议来说,服务端给一 ...