hdu4287 Intelligent IME--哈希表

来源:互联网 发布:java socket 心跳实例 编辑:程序博客网 时间:2024/05/14 20:00

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=4287


一:原题内容

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
13 5466444874goinnightmightgn
 
Sample Output
320

二:分析理解

这题不难,直接看代码吧。

三:AC代码

#include<iostream>    #include<string.h>  #include<algorithm>  using namespace std;int num[5005];char word[5005][8];int hash1[1000010];char kind[] = "22233344455566677778889999";int T;int n, m;int main(){scanf("%d", &T);while (T--){scanf("%d%d", &n, &m);memset(hash1, 0, sizeof(hash1));for (int i = 0; i < n; i++)scanf("%d", &num[i]);for (int i = 0; i < m; i++){scanf("%s", word[i]);int len = strlen(word[i]);int digits = 0;for (int j = 0; j < len; j++)digits = digits * 10 + kind[word[i][j] - 'a'] - '0';        for (int j = 0; j < n; j++){if (num[j] == digits){hash1[digits]++;break;}}}for (int i = 0; i < n; i++)printf("%d\n", hash1[num[i]]);}return 0;}


1 0
原创粉丝点击