2015
05-23

# Intelligent IME

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?

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.

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.

1
3 5
46
64448
74
go
in
night
might
gn

3
2
0

#include<stdio.h>
#include<string.h>
int hash[128];
struct TrieNode{
int no;//对应的数字串的编号，这个编号利于后面对出现次数进行统计
TrieNode *next[10];
}node[30005];
TrieNode *root = &node[0];
int cnt,result[5005];
char word[10],s[10];
void init()
{
//每个字母对应的数字
hash['a']=hash['b']=hash['c']=2;
hash['d']=hash['e']=hash['f']=3;
hash['g']=hash['h']=hash['i']=4;
hash['j']=hash['k']=hash['l']=5;
hash['m']=hash['n']=hash['o']=6;
hash['p']=hash['q']=hash['r']=hash['s']=7;
hash['t']=hash['u']=hash['v']=8;
hash['w']=hash['x']=hash['y']=hash['z']=9;
}
void initRoot()
{
int i;
for(i=0;i<10;i++)
{
root->next[i]=NULL;
}
}
//向字典树中插入新的串。
void insert(char str[],int num)
{
TrieNode *p = root;
int len=strlen(str),i,j;
for(i=0;i<len;i++)
{
if(p->next[str[i]-'0']==NULL)
{
p->next[str[i]-'0']=&node[cnt];
for(j=0;j<10;j++)node[cnt].next[j]=NULL;
node[cnt].no=-1;
cnt++;
}
p=p->next[str[i]-'0'];
}
//设置数字串对应的编号
p->no=num;
}
/** 查询一个字母字符串对应的数字串 */
void query(char str[])
{
int len=strlen(str),i;
TrieNode *p=root;
for(i=0;i<len;i++)
{
p=p->next[hash[str[i]]];
if(p==NULL)break;
}
if(p==NULL)return;//该串对应的数字串不存在。
else{
if(p->no!=-1)result[p->no]++;
}
}
int main()
{
int t,m,n,i;
scanf("%d",&t);
init();
while(t--)
{
cnt=1;
initRoot();
memset(result,0,sizeof(result));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",word);
insert(word,i);
}
for(i=0;i<m;i++)
{
scanf("%s",s);
query(s);
}
for(i=0;i<n;i++)
{
//顺序打出每个串出现的次数。
printf("%d\n",result[i]);
}
}
return 0;
}

1. 没想到觉醒如此小肚鸡肠，听不得任何反面意见，评论功能就是允许别人对某篇文章进行评论发表自己的看法，难道网站上每篇文章下面清一色的支持就是好？你们这是要搞和谐吗？思维碰撞才能出火花，你们也是喜欢P民直接不加过滤的接受你们的暗示和引导？