hdu----(2222)Keywords Search(trie树)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35683 Accepted Submission(s): 11520
Problem 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
Author
Wiskey
代码:
字典树
1 /*hdu 2222 字典树*/
2 //#define LOCAL
3 #include<cstdio>
4 #include<cstring>
5 #include<iostream>
6 using namespace std;
7 struct Trie
8 {
9 struct Trie *next[26];
10 int tail;
11 };
12 char str[55];
13 char ss[1000005];
14 void in_Trie(char *s,Trie *root)
15 {
16 Trie *cur=root,*newcur;
17 for(int i=0;s[i]!=' ';i++)
18 {
19 if(cur->next[s[i]-'a']==NULL)
20 {
21 newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie)));
22 for(int j=0;j<26;j++)
23 newcur->next[j]=NULL;
24 newcur->tail=0;
25 cur->next[s[i]-'a']=newcur;
26 }
27 cur=cur->next[s[i]-'a'];
28 }
29 cur->tail++;
30 }
31 int query(char *s,Trie *root)
32 {
33 int i=0,cnt=0;
34 Trie *cur;
35 for(int j=0;s[j]!=' ';j++)
36 {
37 cur=root;
38 for(i=j;s[i]!=' ';i++){
39 if(cur->next[s[i]-'a']!=NULL){
40 cur=cur->next[s[i]-'a'];
41 cnt+=cur->tail;
42 cur->tail=0; //只需求出第一次出现的
43 }
44 else break;
45 }
46 }
47 return cnt;
48 }
49 void dele(Trie *root)
50 {
51 for(int i=0 ; i<26 ; i++ )
52 if(root->next[i]!=NULL)
53 dele(root->next[i]);
54 // free(root);
55 delete root;
56 }
57 int main()
58 {
59 #ifdef LOCAL
60 freopen("test.in","r",stdin);
61 #endif
62 int t,i,n;
63 Trie *root;
64 scanf("%d",&t);
65 while(t--)
66 {
67 root = new Trie ;
68 for(int j=0;j<26;j++)
69 root->next[j]=NULL;
70 root->tail=0;
71 scanf("%d",&n);
72 for(i=0;i<n;i++){
73 scanf("%s",str);
74 in_Trie(str,root);
75 }
76 scanf("%s",ss);
77 printf("%dn",query(ss,root));
78 dele(root);
79 }
80 return 0;
81 }
- 号称「永远不会输钱」的马丁格尔策略
- 小程序中图片高度等比缩放
- 三分钟使用 Python 处理 Nginx 日志
- Python,Shell 和 三个标准文件
- 【微信官方】获取用户信息方案介绍
- 【实战】如何使用 Python 从 Redis 中删除 4000万 KEY
- [多图] DevOps 也要懂点 Excel
- [实战篇] Python 运维中使用并发
- PHP数据结构(十) ——有向无环图与拓扑算法
- PHP数据结构(十一) ——图的连通性问题与最小生成树算法(1)
- 优化 MySQL: 3 个简单的小调整
- PHP数据结构(十一) ——图的连通性问题与最小生成树算法(2)
- 进程间通信的历史与未来
- PHP数据结构(十二) ——静态查找表
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 强网杯2019-高明的黑客
- CISCN2019华北赛区Day2-HackWorld
- ZJCTF-NiZhuanSiWei
- xxe漏洞学习
- De1CTF2019-SSRFME
- BJDCTF2nd-EasyMd5
- BJDCTF2nd-fakegoogle
- java_Scanner类、Random类、ArrayList 类的使用
- 使用Python获取Oracle索引信息
- 监控Oracle数据泵状态
- MySQL MHA部署 Part 5 MHA部署指南
- MySQL MHA部署 Part 6 MHA故障转移测试
- 一步步搭建基于GTID的MySQL复制
- innobackupex命令总结
- 使用sysbench对数据库进行压测 Part2 sysbench语法介绍