Keywords Search

Description

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.       Wiskey also wants to bring this feature to his image retrieval system.       Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.       To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

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

Print how many keywords are contained in the description.

Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3
 
这个题目看到数据规模,第一反应是用字典树来存放。然后每个结点需要一个权值来记录是否是一个关键词的末端,还有一个权值需要判断单词是否被查找过。
但是这个题目需要注意两点(被坑的两点): 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(字典树)的更多相关文章

  1. hdu----(2222)Keywords Search(trie树)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  3. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  4. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  5. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  6. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  7. ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))

    http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...

  8. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  9. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

随机推荐

  1. jQuery--基础(操作标签)

    jQuery-样式操作 .css() 可以直接使用来获取css的值   .css("color")     使用方法,如果想给查找到的标签添加样式: .css("colo ...

  2. hdu 2063过山车

    二分匹配 #include<iostream> #include<cstring> using namespace std; int k,m,n; int rem[500+5] ...

  3. 微服务之旅:从Netflix OSS到 Istio Service Mesh

    在这篇文章中,我们从Netflix开始,通过Envoy和Istio的崛起,快速浏览微服务的历史. 微服务是具有边界上下文的松散耦合服务,使您能够独立开发,部署和扩展服务.它还可以定义为构建独立开发和部 ...

  4. HDFS源码分析DataXceiver之整体流程

    在<HDFS源码分析之DataXceiverServer>一文中,我们了解到在DataNode中,有一个后台工作的线程DataXceiverServer.它被用于接收来自客户端或其他数据节 ...

  5. linux下安装rabbitmq的rpm包问题记录

    安装rabbitmq的文章和帖子多如牛毛,不管是官网还是各个博客,这里附个Rabbitmq官网安装Rpm包的链接, http://www.rabbitmq.com/install-rpm.html 不 ...

  6. 02 redis通用命令操作

    set hi hello 设置值 get hi 获取值 keys * 查询出所有的key memcached 不能查询出所有的key keys *h 模糊查找key keys h[ie] 模糊查找 k ...

  7. PHP 获取网络接口文件流

    获取网络接口里面的文件流 php开发调用各种接口在所难免,有时须要传递非常多參数. 在传递參数过程中 '&' 有时会被 解析成 '&'导致请求失败 经过查找资料和比較,发现php提供了 ...

  8. MaLoc: a practical magnetic fingerprinting approach to indoor localization using smartphones

    https://www.indooratlas.com/ MaLoc: a practical magnetic fingerprinting approach to indoor localizat ...

  9. CentOS7的/tmp目录自动清理规则(转)

    CentOS7的/tmp目录自动清理规则 CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用sys ...

  10. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...