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数字各自代表了自己的字母,求给出的几个数字串分别能够打出下列几个英文单词。
思路:这道题的话,我们用字典树来做,我们首先先建一个字典树,然后将数字插入到字典树中,并赋予color值。然后将字母对应数字,因为字母对应唯一的数字,这样换成数字就可以进行查询了,然后再记一下数就可以了。
AC代码:
#include <bits/stdc++.h>
typedef long long ll;
const int maxx=500010;
const int maxm=5010;
const int maxn=10;
const int inf=0x3f3f3f3f;
using namespace std;
int Trie[maxx][maxn];
int color[maxx];
char s[maxn];
int a[maxm];
char p[30]="22233344455566677778889999";
int k;
void init()
{
memset(Trie,0,sizeof(Trie));
memset(color,0,sizeof(color));
k=0;
}
void insert(char *w,int q)
{
int p=0;
int len=strlen(w);
for(int i=0; i<len; i++)
{
int c=w[i]-'a';
if(!Trie[p][c])
Trie[p][c]=++k;
p=Trie[p][c];
}
color[p]=q;
}
int search(char *w)
{
int p=0;
int len=strlen(w);
for(int i=0; i<len; i++)
{
int c=w[i]-'a';
if(!Trie[p][c])
return 0;
p=Trie[p][c];
}
return color[p];
}
int main()
{
int t;
scanf("%d",&t);
init();
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++)
{
scanf("%s",s);
insert(s,i);
}
for(int i=1; i<=m; i++)
{
scanf("%s",s);
for(int j=0; j<strlen(s); j++)
s[j]=p[s[j]-'a'];
a[search(s)]++;
}
for(int i=1; i<=n; i++)
printf("%d\n",a[i]);
}
return 0;
}