Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1279    Accepted Submission(s): 669

Problem Description
We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below:

2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o    

7 : p, q, r, s  8 : t, u, v    9 : w, x, y, z

When we want to input the word “wing”, we press the button 9, 4, 6, 4, then the input method will choose from an embedded dictionary, all words matching the input number sequence, such as “wing”, “whoi”, “zhog”. Here comes our question, given a dictionary, how many words in it match some input number sequences?

 
Input
First is an integer T, indicating the number of test cases. Then T block follows, each of which is formatted like this:

Two integer N (1 <= N <= 5000), M (1 <= M <= 5000), indicating the number of input number sequences and the number of words in the dictionary, respectively. Then comes N lines, each line contains a number sequence, consisting of no more than 6 digits. Then comes M lines, each line contains a letter string, consisting of no more than 6 lower letters. It is guaranteed that there are neither duplicated number sequences nor duplicated words.

 
Output
For each input block, output N integers, indicating how many words in the dictionary match the corresponding number sequence, each integer per line.

 
Sample Input
1
3 5
46
64448
74
go
in
night
might
gn
 
Sample Output
3
2
0

字典树题,由于字符串长度小也可以hash,用字典树时注意将字母字符串转换为数字字符串进行构建,不要直接构建原串,然后将数字字符串转换为字母字符串进行查找,这样会超时

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<map>
  9. #include<iomanip>
  10. #define INF 99999999
  11. using namespace std;
  12.  
  13. const int MAX=10;
  14. const int N=5000+10;
  15. char s[N][8],ch[8];
  16. char d[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
  17. int sum;
  18.  
  19. struct TrieNode{
  20. int num;
  21. TrieNode *next[MAX];
  22. TrieNode(){
  23. num=0;
  24. memset(next,0,sizeof next);
  25. }
  26. }root;
  27.  
  28. void InsertNode(char *a){
  29. int k=0;
  30. TrieNode *p=&root;
  31. while(a[k]){
  32. if(!p->next[a[k]-'0'])p->next[a[k]-'0']=new TrieNode;
  33. p=p->next[a[k++]-'0'];
  34. ++p->num;
  35. }
  36. }
  37.  
  38. int SearchTrie(char *a){
  39. int k=0;
  40. TrieNode *p=&root;
  41. while(a[k] && p->next[a[k]-'0'])p=p->next[a[k++]-'0'];
  42. if(a[k])return 0;
  43. return p->num;
  44. }
  45.  
  46. void trans(char *a){
  47. int len=strlen(a);
  48. for(int i=0;i<len;++i){
  49. if(a[i] == 'a' || a[i] == 'b' || a[i] == 'c')a[i]='2';
  50. else if(a[i] == 'd' || a[i] == 'e' || a[i] == 'f')a[i]='3';
  51. else if(a[i] == 'g' || a[i] == 'h' || a[i] == 'i')a[i]='4';
  52. else if(a[i] == 'j' || a[i] == 'k' || a[i] == 'l')a[i]='5';
  53. else if(a[i] == 'm' || a[i] == 'n' || a[i] == 'o')a[i]='6';
  54. else if(a[i] == 'p' || a[i] == 'q' || a[i] == 'r' || a[i] == 's')a[i]='7';
  55. else if(a[i] == 't' || a[i] == 'u' || a[i] == 'v')a[i]='8';
  56. else if(a[i] == 'w' || a[i] == 'x' || a[i] == 'y' || a[i] == 'z')a[i]='9';
  57. else a[i]='1';
  58. }
  59. }
  60.  
  61. void Free(TrieNode *p){
  62. for(int i=0;i<10;++i)if(p->next[i])Free(p->next[i]);
  63. delete p;
  64. }
  65.  
  66. int main(){
  67. int t,n,m;
  68. cin>>t;
  69. while(t--){
  70. cin>>n>>m;
  71. for(int i=0;i<n;++i)scanf("%s",s[i]);
  72. for(int i=0;i<m;++i){
  73. scanf("%s",ch);
  74. trans(ch);
  75. InsertNode(ch);
  76. }
  77. for(int i=0;i<n;++i)printf("%d\n",SearchTrie(s[i]));
  78. for(int i=0;i<10;++i){
  79. if(root.next[i])Free(root.next[i]);
  80. root.next[i]=0;
  81. }
  82. }
  83. return 0;
  84. }

hdu4284之字典树的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. 皴EBS R12应用程序和数据库用户password

    1.假设你有一个EBS周围环境APPS用户password,能够打破用户的应用程序password 参考:Oracle EBS R12下怎样破解用户password 2,假设没有APPS用户passw ...

  2. LibVLC audio controls

    原文 http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__audio.html LibVLC audio co ...

  3. Mac OSX操作系统安装和配置Zend Server 6教程(1)

    作为web开发人员,应该熟悉掌握各种系统下安装和配置web服务器与站点的技术. 随着越来越多的开发人员选择Zend Server服务器,慧都推出了在Mac OSX系统安装和配置Zend Server ...

  4. Juqery遮罩插件

    Juqery遮罩插件,想罩哪就罩哪!   一  前言 在项目开发时发现没有一个用起来 爽一点的遮罩插件,看起来觉得不难 好吧那就利用空闲时间,自己折腾一个吧,也好把jquery再温习一下, 需要的功能 ...

  5. cocos2d的-X- luaproject的LUA脚本加密

    2014/1/26 更新 近期又发现了一个非常easy的方法,事实上coco2dx已经给我们提供设置loader的方法. 注意:有个局限性,在非android平台下调用pEngine->exec ...

  6. Mysql高级之索引

    原文:Mysql高级之索引 索引:是针对数据所建立的目录. 作用: 可以加快查询速度 负面影响: 降低了增删改的速度. 索引的创建原则: 1:不要过度索引 2:在where条件最频繁的列上加.在重复度 ...

  7. JavaScript中的execCommand()命令详解及实例展示

    execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其 ...

  8. jsonp总结

    由于“同源策略”的限制,ajax不能做跨域请求,jsonp是当下解决跨域请求最流行的方案,来个例子(index.html): <!doctype html> <html lang=& ...

  9. [转]理解C# 4 dynamic(1) - var, object, dynamic的区别以及dynamic的使用

    阅读目录: 一. 为什么是它们三个 二. 能够任意赋值的原因 三. dynamic的用法 四. 使用dynamic的注意事项 一. 为什么是它们三个? 拿这三者比较的原因是它们在使用的时候非常相似.你 ...

  10. Unity Container

    Unity Container中的几种注册方式与示例 2013-12-08 22:43 by 小白哥哥, 22 阅读, 0 评论, 收藏, 编辑 1.实例注册 最简单的注册方式就是实例注册,Unity ...