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. initramfs扫描磁盘前改变磁盘上电顺序

    背景: 机械硬盘需要12V 5V电源,此前设计是硬件电路默认5V有效.12V无效,然后系统通过驱动上12V电,对磁盘来说相当于先上5V后上12V,这种方式对大部分磁盘是可以的,但对于日立 HGST磁盘 ...

  2. Chrome自带恐龙小游戏的源码研究(五)

    在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观 ...

  3. vim与sublime

    vim与sublime 对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持 ...

  4. 数据库ACID操作---事务四原则

    事务操作四原则: 1>原子性:简单来说——整个事务操作如同原子已经是物理上最小的单位,不可分离事务操作要么一起成功,要么一起失败. 2>一致性:倘若事务操作失败,则回滚事务时,与原始状态一 ...

  5. JDK设置Encoding编码格式

    执行JAVA程序报错内容如下: java.lang.IllegalStateException: The Java Virtual Machine has not been configured to ...

  6. ASP.NET MVC 相关的社群与讨论区

    ASP.NET MVC 官方论坛  http://forums.asp.net/1146.aspx 台湾微软MSDN论坛 --- ASP.NET 与 AJAX(ASP.NET AND AJAX)讨论区 ...

  7. spring AOP(切面)

    AOP 基本概念cross  cutting concern横切性关注点:独立服务,遍布在系统处理流程之中 Aspect对横切关注点模块化 advice对横切关注点具体实现 pointcut定义adv ...

  8. memcached系列

    memcached系列:http://blog.csdn.net/xingxing513234072/article/category/2462865

  9. js 获取地理位置经纬度

    1. 加载百度API的核心js,ak表示获取百度地图的开发密钥,免费的需要申请下 <script type="text/javascript" src="http: ...

  10. 【题解】AT2043 AND Grid

    [题解]AT2043 AND Grid 我们考虑直接构造两个互补的图切分别联通的图,然后原图有的大家都有就构造完成了. #include<iostream> #include<cst ...