hdu Intelligent IME
2015-12-03 17:16
399 查看
算法:字典树
题意:手机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之间建树,查找就方便多了;
代码:
题意:手机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; }
相关文章推荐
- 使用 Gradle 和 Sonatype Nexus 搭建自己的 Maven 仓库
- Android studio编译错误 duplicate files during packaging of APK...error
- AFNetworking上传图片
- iOS-数据持久化-第三方框架FMDB的使用
- 实验三进程调度实验
- 我的css reset
- hdu Intelligent IME
- jqueryui简单tree的使用(笔记,未完待续……)
- json解析异常
- leetcode Pascal's Triangle II
- HttpClient进行POST请求(HTTPS方式)
- AMBA、AHB、APB总线简介
- android 4.0 中内置卡创建文件
- Python程序设计课程教学方法与教学重点探讨
- 文件下载公共方法 以及调用
- 2015杭电新生赛1001搬砖【dfs】
- iOS UITabBarController 典型应用
- QT 相关问题
- 关于神一样存在的csdn-一个bug的修复时间是以年记?而且目前还没解决!!!
- nagios配置,Linux下Nagios的工作原理简介