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
Source
题解:手机九宫格,字母与对应的数字.... 自己过了案例,但是还是WA,之后才知道自己写的代码不能判断相等的情况....srO
WA代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 13 struct node 14 { 15 int count; 16 node *next[200]; 17 node(){ //构造函数 18 count=0; 19 memset(next,0,sizeof(next)); 20 } 21 }; 22 node *root; 23 int k=0; 24 void insert(char *a) 25 { 26 int l=strlen(a); 27 node *p=root; 28 int i; 29 for(i=0;i<l;i++) 30 { 31 if(p->next[a[i]-'a']==0) 32 { 33 p->next[a[i]-'a']=new node; 34 if(a[i]<'a'&&a[i]>'z') 35 p->next[a[i]-'a']->count=p->count+0; 36 if(a[i]>='a'&&a[i]<='c') 37 p->next[a[i]-'a']->count+=p->count+2; 38 if(a[i]>='d'&&a[i]<='f') 39 p->next[a[i]-'a']->count+=p->count+3; 40 if(a[i]>='g'&&a[i]<='i') 41 p->next[a[i]-'a']->count+=p->count+4; 42 if(a[i]>='j'&&a[i]<='l') 43 p->next[a[i]-'a']->count+=p->count+5; 44 if(a[i]>='m'&&a[i]<='o') 45 p->next[a[i]-'a']->count+=p->count+6; 46 if(a[i]>='p'&&a[i]<='s') 47 p->next[a[i]-'a']->count+=p->count+7; 48 if(a[i]>='t'&&a[i]<='v') 49 p->next[a[i]-'a']->count+=p->count+8; 50 if(a[i]>='w'&&a[i]<='z') 51 p->next[a[i]-'a']->count+=p->count+9; 52 } 53 // 已存在此前缀 54 p=p->next[a[i]-'a']; 55 } 56 } 57 int find(char *s) 58 { 59 struct node *p; 60 int len=strlen(s); 61 if(len==0) return 0; 62 p=root; 63 for(int i=0;i<len;i++){ 64 if(p->next[s[i]-'a']!=0) 65 p=p->next[s[i]-'a']; 66 else 67 return 0; 68 } 69 return p->count; 70 } 71 void de(node *p) 72 { 73 if(p==0) 74 return ; 75 int i; 76 for(i=0;i<26;i++) 77 { 78 de(p->next[i]); 79 } 80 delete p; 81 82 } 83 int main() 84 { 85 int t; 86 scanf("%d",&t); 87 char a[30]; 88 int b[5050],s[5050]; 89 while(t--) 90 { 91 root = new node; 92 memset(b,0,sizeof(b)); 93 memset(s,0,sizeof(s)); 94 memset(a,'