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?
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.
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,用字典树时注意将字母字符串转换为数字字符串进行构建,不要直接构建原串,然后将数字字符串转换为字母字符串进行查找,这样会超时
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<iomanip> #define INF 99999999 using namespace std; const int MAX=10; const int N=5000+10; char s[N][8],ch[8]; char d[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; int sum; struct TrieNode{ int num; TrieNode *next[MAX]; TrieNode(){ num=0; memset(next,0,sizeof next); } }root; void InsertNode(char *a){ int k=0; TrieNode *p=&root; while(a[k]){ if(!p->next[a[k]-'0'])p->next[a[k]-'0']=new TrieNode; p=p->next[a[k++]-'0']; ++p->num; } } int SearchTrie(char *a){ int k=0; TrieNode *p=&root; while(a[k] && p->next[a[k]-'0'])p=p->next[a[k++]-'0']; if(a[k])return 0; return p->num; } void trans(char *a){ int len=strlen(a); for(int i=0;i<len;++i){ if(a[i] == 'a' || a[i] == 'b' || a[i] == 'c')a[i]='2'; else if(a[i] == 'd' || a[i] == 'e' || a[i] == 'f')a[i]='3'; else if(a[i] == 'g' || a[i] == 'h' || a[i] == 'i')a[i]='4'; else if(a[i] == 'j' || a[i] == 'k' || a[i] == 'l')a[i]='5'; else if(a[i] == 'm' || a[i] == 'n' || a[i] == 'o')a[i]='6'; else if(a[i] == 'p' || a[i] == 'q' || a[i] == 'r' || a[i] == 's')a[i]='7'; else if(a[i] == 't' || a[i] == 'u' || a[i] == 'v')a[i]='8'; else if(a[i] == 'w' || a[i] == 'x' || a[i] == 'y' || a[i] == 'z')a[i]='9'; else a[i]='1'; } } void Free(TrieNode *p){ for(int i=0;i<10;++i)if(p->next[i])Free(p->next[i]); delete p; } int main(){ int t,n,m; cin>>t; while(t--){ cin>>n>>m; for(int i=0;i<n;++i)scanf("%s",s[i]); for(int i=0;i<m;++i){ scanf("%s",ch); trans(ch); InsertNode(ch); } for(int i=0;i<n;++i)printf("%d\n",SearchTrie(s[i])); for(int i=0;i<10;++i){ if(root.next[i])Free(root.next[i]); root.next[i]=0; } } return 0; }
作者:xingyeyongheng 发表于2013-7-23 0:01:45 原文链接
阅读:139 评论:0 查看评论