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 ...
随机推荐
- Java Enum的使用
最近为了便于对状态码的描述信息进行解析,学习了一下Enum的使用,发现还挺好使的. 首先,定义一个Enum的类Status,有两个属性statusValue状态码 以及 statusDesc状态描述 ...
- grep命令:查看配置文件未注释行(转)
FROM: https://linux.cn/article-6958-1.html 可以使用 UNIX/BSD/OS X/Linux 这些操作系统自身提供的 grep,sed,awk,perl或者其 ...
- LeetCode(100)题解--Same Tree
https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...
- H2 应用实例1
说明:本例子开发工具为NetBeans,jdk 1.7 进行测试说明 H2安装说明如下 1. H2数据库必要文件下载地址为: http://www.h2database.com (1) 下载截 ...
- 计算机网络 --万维网www
万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...
- 九度OJ 1168:字符串的查找删除 (查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...
- 九度OJ 1050:完数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7535 解决:3125 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+ ...
- cocos2d-js添加艾盟插屏(通过jsb反射机制)
1.导入jar包 2.修改AndroidManifest.xml文件 添加: <activity android:name="com.xingka ...
- 前端mvc组合框架
1. jquery 2. underscore 3. backbone 4. reactjs 5. seajs
- 性能测试--Jmeter随机生成/随机选取/csv读取关键字
Jmeter随机生成/随机选取/csv读取关键字 一.随机生成关键字 随机生成关键字,需要组件:随机变量配置元件(Random Variable) 该组件的作用是生成字符+随机数字格式的字符串,并保 ...