Intelligent IME
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1348 Accepted Submission(s): 685
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
题意:根据题目中给的。小写字母都可以对应成一个数字。。先输入一个n,一个m,接下去输入n个号码,m个小写字母组成的号码。要求出n个号码中每个在m个小写字母中出现的次数。。
思路:。由于只有6位数。直接开个100W的数组标记每个号码就可以了。。 每次小写字母转换成的号码就在该号码加一。最后输出就可以了。。
代码:
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
int t;
int n, m;
int vis[1000];
int sb[5005];
int hash[1000005];
int main() {
vis['a'] = vis['b'] = vis['c'] = 2;
vis['d'] = vis['e'] = vis['f'] = 3;
vis['g'] = vis['h'] = vis['i'] = 4;
vis['j'] = vis['k'] = vis['l'] = 5;
vis['m'] = vis['n'] = vis['o'] = 6;
vis['p'] = vis['q'] = vis['r'] = vis['s'] = 7;
vis['t'] = vis['u'] = vis['v'] = 8;
vis['w'] = vis['x'] = vis['y'] = vis['z'] = 9;
scanf("%d", &t);
while (t --) {
memset(hash, 0, sizeof(hash));
memset(sb, 0, sizeof(sb));
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++)
scanf("%d", &sb[i]);
getchar();
for (int i = 0; i < m; i ++) {
char sbbb[10];
int num = 0;
gets(sbbb);
for (int j = 0; j < strlen(sbbb); j ++)
num = num * 10 + vis[sbbb[j]];
hash[num] ++;
}
for (int i = 0; i < n; i ++)
printf("%d\n", hash[sb[i]]);
}
return 0;
}