HDOJ---4287 Intelligent IME[字典树---水题]

Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 145    Accepted Submission(s): 80


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
 

 

Source
 

 

Recommend
liuyiding
 
 
 
题意:下面的字符串中,有多少个是按照相应的数字输入顺序得到的。
 
code:
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int n,m;
 5 char str[5100][8];
 6 int ans;
 7 
 8 struct node
 9 {
10     int cnt;
11     node *next[26];
12 }*p;
13 
14 int key[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
15 
16 void build(char str[],int k,node *head)
17 {
18     while(k<strlen(str))
19     {
20         if(head->next[str[k]-'a']!=NULL)
21         {
22             head->next[str[k]-'a']->cnt+=1;
23             head=head->next[str[k]-'a'];
24         }
25         else
26         {
27             head->next[str[k]-'a']=new node;
28             head=head->next[str[k]-'a'];
29             head->cnt=1;
30             for(int i=0;i<26;i++)
31                 head->next[i]=NULL;
32         }
33         k++;
34     }
35 }
36 
37 void search(char str[],int k,node *head)
38 {
39     if(head==NULL)
40         return;
41     if(k==strlen(str))
42     {
43         ans+=head->cnt;
44         return;
45     }
46     for(int i=0;i<26;i++)
47     {
48         if(key[i]-(str[k]-'0')==0)
49         {
50             search(str,k+1,head->next[i]);
51         }
52     }
53 }
54 
55 int main()
56 {
57     int t;
58     scanf("%d",&t);
59     while(t--)
60     {
61         p=new node;
62         for(int i=0;i<26;i++)
63             p->next[i]=NULL;
64         scanf("%d%d",&n,&m);
65         for(int i=0;i<n;i++)
66             scanf("%s",str[i]);
67         for(int i=0;i<m;i++)
68         {
69             char strt[20];
70             scanf("%s",strt);
71             build(strt,0,p);
72         }
73         for(int i=0;i<n;i++)
74         {
75             ans=0;
76             search(str[i],0,p);
77             printf("%d\n",ans);
78         }
79     }
80     return 0;
81 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值