HDU 4287-Intelligent IME(哈希)

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

1
3 5
46
64448
74
go
in
night
might
gn

Sample Output

3
2
0

题意 :在手机键盘的背景下,给出一串数字,然后由这些数字可以枚举出一系列的字符串,然后给出一个字典,问这些字符串一共有几个在字典中,逆向思考一下,一开始我是考虑着由给出的数字枚举出所有的可能的字符串然后在字典中一一查找,但时间复杂度太高了,后来看到题解才明白,字符串是可以转换成数字的,而且唯一对应,然后哈希一下就可以了。
#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 long
using 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;
}

时间: 2024-10-14 04:42:49

HDU 4287-Intelligent IME(哈希)的相关文章

HDU 4287 Intelligent IME(map运用)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2091    Accepted Submission(s): 1031

HDU 4287 Intelligent IME(字典树数组版)

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4776    Accepted Submission(s): 2227 Problem Description We all use cell phone today. And we must be familiar with the intelligen

HDU 4287 Intelligent IME hash

Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4287 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,

HDU 4287 Intelligent IME (字典树 &amp;&amp; map)

分析:此题题意很简单,我就不说了,第一想到的就是用字典树做,首先你得考虑用哪个作为字典树,显然,这里用后面的字符串作为树更好. 接着就是套模板了.此题WA了无数次,找bug找了一天,也没找到头绪,后来看别人的结题报告,才明白,原来坑爹的把memset(mark)放在了循环外面,一直以为只循环一次就够了,,真是坑爹啊,,, 做到现在了,感觉字典树的问题基本上都能用map来解决. 代码一:字典树 学会了用内联,确实会快一些 #include <iostream> #include <cstr

hdu 4287 Intelligent IME

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 

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

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

杭电(hdu)ACM 4287 Intelligent IME

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3453    Accepted Submission(s): 1647 Problem Description We all use cell phone today. And we must be familiar with the intelligen

hdu 4278 Intelligent IME

Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3487    Accepted Submission(s): 1659 Problem Description We all use cell phone today. And we must be familiar with the intelligen

hdu acm 1425 sort(哈希表思想)

sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25803    Accepted Submission(s): 7764 Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且