Intelligent ime
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2479 accepted submission (s): 1212
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 question: in the background of the mobile phone keyboard, a string of numbers can be given, and then a dictionary can be given, there are a few of these strings in the dictionary. In the beginning, I thought about enumerating all the possible strings by the given number and then searching them one by one in the dictionary, however, the time complexity is too high. Later I saw the problem solution to understand that the string can be converted into numbers, and the only corresponding character can be hashed.#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define L long longusing namespace std;const int INF=0x3f3f3f3f;const int maxn=1000020;int n,m,num[maxn],h[maxn];char phone[]="22233344455566677778889999";char word[5010][7];int Binary_search(int x){int mid,low=0,high=n-1;while(low<=high){mid=(low+high)/2;if(num[mid]==x)return mid;else if(num[mid]>x)high=mid-1;else if(num[mid]<x)low=mid+1;}return -1;}int main(){int t;scanf("%d",&t);while(t--){memset(h,0,sizeof(h));scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%d",num+i);for(int i=0;i<m;i++)scanf("%s",word[i]);vector <int> pos(num,num+n);sort(num,num+n);for(int i=0;i<m;i++){int len=strlen(word[i]);int sum=0;for(int j=0;j<len;j++)sum=sum*10+(phone[word[i][j]-'a']-'0');int tem=Binary_search(sum); if(tem!=-1)h[num[tem]]++;}for(int i=0;i<n;i++)printf("%d\n",h[pos[i]]);}return 0;}
HDU 4287-intelligent IME (hash)