hdu 4287 Intelligent IME (字典树+dfs)

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
 
代码(1):字典树+bfs
 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 #define MAXN 5005
 5 typedef struct node
 6 {
 7     node * next[26];
 8     int num;
 9     node()
10     {
11         num=0;
12         memset(next,NULL,sizeof(next));
13     }
14 }Nod;
15 int ans;
16 char ask[MAXN][10];
17 char stemp[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
18 void insert_tree(Nod * head,char *str)
19 {
20     Nod *h=head;
21     int len=strlen(str);
22     int i;
23     for(i=0;i<len;i++)
24     {
25         int id=str[i]-'a';
26         if(h->next[id]==NULL)
27             h->next[id]=new node;
28         h=h->next[id];
29     }
30     h->num++;
31 }
32 void find_tree(Nod *head,char *str,int x,int l)
33 {
34     if(x>=l)
35     {
36         ans+=head->num;
37         return;
38     }
39     int j;
40     Nod *h=head;
41     int id=str[x]-'0';
42     for(j=0;j<strlen(stemp[id]);j++)
43     {
44         int dx=stemp[id][j]-'a';
45         if(h->next[dx]!=NULL)
46             find_tree(h->next[dx],str,x+1,l);
47     }
48 }
49 void freedom(Nod *h)
50 {
51     int i;
52     for(i=0;i<26;i++)
53     {
54         if(h->next[i]!=NULL)
55             freedom(h->next[i]);
56     }
57     delete h;
58 }
59 int main()
60 {
61     char tstr[10];
62     int t;
63     scanf("%d",&t);
64     while(t--)
65     {
66         int i,n,m;
67         scanf("%d%d",&n,&m);
68         getchar();    
69         Nod *head;
70         head=new node;
71         for(i=0;i<n;i++)
72             gets(ask[i]);
73         for(i=0;i<m;i++)
74         {
75             gets(tstr);
76             insert_tree(head,tstr);
77         }
78         for(i=0;i<n;i++)
79         {
80             ans=0;
81             find_tree(head,ask[i],0,strlen(ask[i]));
82             printf("%d\n",ans);
83         }
84         freedom(head);
85     }
86     return 0;
87 }

 代码(2):利用映射关系直接求解

 1 #include<iostream>
 2 using namespace std;
 3 int ax[5005];
 4 int a[]={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};
 5 int mark[5005];
 6 int main()
 7 {
 8     int t;
 9     scanf("%d",&t);
10     while(t--)
11     {
12         int n,m,x;
13         scanf("%d%d",&n,&m);
14         int i;
15         for(i=1;i<=n;i++)
16         {
17             scanf("%d",&x);
18             ax[i]=x;
19         }
20         int j,s;
21         char str[10];
22         memset(mark,0,sizeof(mark));
23         for(i=1;i<=m;i++)
24         {
25             scanf("%s",str);
26             for(s=j=0;str[j];j++)
27                 s=s*10+a[str[j]-'a'];
28             for(j=1;j<=n;j++)
29                 if(s==ax[j])
30                     mark[j]++;
31         }
32         for(i=1;i<=n;i++)
33             printf("%d\n",mark[i]);            
34     }
35     return 0;
36 }

 代码(3):将代码(2)改成map容器,可以优化很多时间和空间

 
 1 #include<iostream>
 2 #include<map>
 3 using namespace std;
 4 int a[]={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};
 5 int mark[5005];
 6 int main()
 7 {
 8     int t;
 9     map<int,int> ma;
10     scanf("%d",&t);
11     while(t--)
12     {
13         int n,m,x;
14         scanf("%d%d",&n,&m);
15         int i;
16         for(i=1;i<=n;i++)
17         {
18             scanf("%d",&x);
19             ma[x]=i;      //
20         }
21         int j,s;
22         char str[10];
23         memset(mark,0,sizeof(mark));
24         for(i=1;i<=m;i++)
25         {
26             scanf("%s",str);
27             for(s=j=0;str[j];j++)
28                 s=s*10+a[str[j]-'a'];
29             mark[ma[s]]++;   //
30         }
31         for(i=1;i<=n;i++)
32             printf("%d\n",mark[i]);            
33     }
34     return 0;
35 }

 

posted @ 2012-09-10 21:06  crazy_apple  阅读(350)  评论(0编辑  收藏  举报