算法:字典树

题意:手机9键拼音:2:abc  3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz;

第一行读入一个T,代表测试组数;

之后输入两个整数n和m,

有n个数字串和m个字符串

让你判断给定的数字串可以得到几种字符组合,在给定的字符串中;并输出相应的个数;

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

思路:用字符串在数字2-9之间建树,查找就方便多了;

代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <cstring>
using namespace std;
#define Max 13
struct dot
{dot *next[Max];int flag;
} ;
dot *newnode()
{dot *temp=new dot;temp->flag=0;for(int i=0;i<Max;i++)temp->next[i]=NULL;return temp;
}
void tree(char *st,dot *root)
{dot *p=root;int id=0;int len=strlen(st);for(int i=0;i<len;i++){if(st[i]>='a'&&st[i]<='c')id=2;else if(st[i]>='d'&&st[i]<='f')id=3;else if(st[i]>='g'&&st[i]<='i')id=4;else if(st[i]>='j'&&st[i]<='l')id=5;else if(st[i]>='m'&&st[i]<='o')id=6;else if(st[i]>='p'&&st[i]<='s')id=7;else if(st[i]>='t'&&st[i]<='v')id=8;else if(st[i]>='w'&&st[i]<='z')id=9;if(p->next[id]==NULL)p->next[id]=newnode();p=p->next[id];}p->flag++;
}
int find(char *st,dot *root)
{int id;dot *p=root;for(int i=0;i<strlen(st);i++){id=st[i]-'0';if(p->next[id]==NULL)return 0;p=p->next[id];}return p->flag;
}
void del(dot *t)
{if(t==NULL) return ;for(int i=0;i<Max;i++)if(t->next[i]!=NULL)del(t->next[i]);delete t;
}
int main()
{int n,m,i,j,k,t;cin>>t;char s[5005][20],st[30];while(t--){cin>>n>>m;dot *root=new dot;root=newnode();for(i=0;i<n;i++)cin>>s[i];while(m--){cin>>st;tree(st,root);}for(i=0;i<n;i++)cout<<find(s[i],root)<<endl;del(root);}return 0;
}

转载于:https://www.cnblogs.com/wangyumin/p/5323467.html

hdu Intelligent IME相关推荐

  1. HDU-4287 Intelligent IME

    题目:Intelligent IME Problem Description We all use cell phone today. And we must be familiar with the ...

  2. hdu 4287 sdnu 1119 Intelligent IME

    原题链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=4287 sdnu: http://210.44.14.31/problem/show/111 ...

  3. HDU -4287 Intelligent IME 字典树数组模拟

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

  4. HDU4287 Intelligent IME(Trie树,map)

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

  5. dd实现Linux转移,linux命令-dd {拷贝并替换}

    一 命令解释: dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: if=文 ...

  6. 字典树,01字典树,可持续化01字典树(总结+例题)

    目录 字典树 01字典树 字典树例题: power oj 2390: 查单词 HDU 1671 Phone List HDU 1004Let the Balloon Rise HDU 1075 Wha ...

  7. wince 6中硬键盘 输入法解决

    在一个项目中,我遇到了9宫格的键盘问题:wince下的keyboard的构架是native 方式,往 系统发送的是数字按键,要在wince上实现汉字或字母,就需要一个输入法来解析! 好在T9就是这样设 ...

  8. 输入法(IME)实现原理

    缘起 在windows下使用的sougou输入法,除了经常弹出广告,没有什么异常行为,Linux下的中文输入法似乎没那么强大好用了.起初用的是Ibus中的pinyin,后来因为机缘巧合装了yong输入 ...

  9. HDU 4775 Infinite Go 并查集

    题目链接:Infinite Go Infinite Go Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. HDU——1106排序(istringstream的使用、STLvector练习)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

最新文章

  1. 如何解决代码中if…else 过多的问题
  2. 一个程序员眼中的好UI
  3. 考研数学早年真题整理20题(很有可能重考!!)
  4. Nightwish全集+6张EP+3张LV+N多图片[320K/MP3]
  5. 每年考研计算机专硕和学硕报比例,各院校研招计划发布 专硕与学硕比例调整...
  6. idea 中maven依赖报错3部曲
  7. 奥鹏东北大学作业答案计算机网络,东北大学17秋学期《计算机网络》在线作业1参考答案...
  8. 微服务架构实战(四):服务发现的可行方案以及实践案例
  9. Atitit 项目沟通管理 艾提拉总结 目录 1. 项目中沟通对象 2 1.1. 主要为项目干系人 产品部门 运营部门组员等 2 1.2. 适当越级沟通, 与高层直接沟通 3 2. 沟通频率 3
  10. PyQT多线程串口 QtDesigner
  11. a与雪碧图在导航处的使用
  12. 实习生的一点建议和感悟
  13. javascript特效3月12日软件速递:Mozilla Firefox发布
  14. 从word2vec开始,说下GPT庞大的家族系谱
  15. Linux远程登录xftp,xshell下载以及简单使用教程
  16. jsp做的留言系统(防止非法登录、增删改查留言)
  17. 教你怎么用c++基本语法实现一个简单的五子棋小游戏
  18. EZ-USB FX2 LP CY7C68013A 开发指南(1)--基本概念
  19. 系统MTU值的更改方法
  20. window下获取指定目录下面的所有文件名 c/c++

热门文章

  1. hdu6287(分解质因数+二分)
  2. 需求分析(知识点总结)
  3. 手机图片怎么压缩变小,值得收藏的几个在线工具
  4. SylixOS之OK6410开发板系统烧写
  5. 恶意软件清理助手V2.6.3 build 005 2007-07-05
  6. 一篇文章 Redis 从 0 到 1
  7. office word ppt 无法打开, 一直处于安全模式,无法解除
  8. 盈利能力分析之-毛利率、销售净利率、投资回报率、权益回报率、资产回报率...
  9. 【iOS】iOS8 与以前版本比较,添加的新特性
  10. BurpSuite工具-HTTP协议详解部分(不懂就查系列)