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;
}

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

  1. HDU 4287 Intelligent IME(map运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...

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

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 4287 Intelligent IME hash

    Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  4. HDU 4287 Intelligent IME

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  6. HDU 4287 Intelligent IME(string,map,stl,make_pair)

    题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...

  7. HDU 4287 Intelligent IME(字典树)

    在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...

  8. Intelligent IME HDU - 4287 字典树

    题意: 给你m个字符串,每一个字符对应一个数字,如下: 2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o ...

  9. hdu Intelligent IME

    算法:字典树 题意:手机9键拼音:2:abc  3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz: 第一行读入一个T,代表测试组数: 之后输入两个整数n和m, 有 ...

随机推荐

  1. 《Java编程思想》笔记 第二十一章 并发

    1.定义任务 实现Runnable 接口的类就是任务类(任务类不一定是实现Runnable接口的类). 实现Runnable 接口,重写run()方法,run方法的返回值只能是 void 任务类就是表 ...

  2. Opencv第三章

    2. 下面这个练习是帮助掌握矩阵类型.创造一个三通道二维矩阵,字节类型,大小为100×100,并设置所有数值为0. a. 在矩阵中使用void cvCircle(CvArr* img, CvPoint ...

  3. Selenium2+python自动化37-爬页面源码(page_source)【转载】

    前言 有时候通过元素的属性的查找页面上的某个元素,可能不太好找,这时候可以从源码中爬出想要的信息.selenium的page_source方法可以获取到页面源码. selenium的page_sour ...

  4. AC日记——[Noi2011]阿狸的打字机 bzoj 2434

    2434 思路: 构建ac自动机: 抽离fail树: 根据字符串建立主席树: 在线处理询问: 询问x在y中出现多少次,等同于y有多少字母的fail能走到x: 1a,hahahahah: 代码: #in ...

  5. js 之面向对象

    对象 是什么? 对象就是一个整体,对外提供一些操作,比如:电视机(不用知道内部的构造,只知道怎么用) 面向对象 是什么? 使用对象时,只关注对象提供的功能,不关心内部细节,比如:操作电视机(只知道用遥 ...

  6. selenium grid 环境搭建

    一.selenium grid简介 selenium grid可以同时在不同机器上测试不同浏览器,包含一个hub和多个node.node会发送配置信息到hub,hub记录并跟踪每一个node的配置信息 ...

  7. [OpenJudge8462][序列DP]大盗阿福

    大盗阿福 总时间限制: 1000ms 内存限制: 65536kB [描述] 阿福是一名经验丰富的大盗.趁着月黑风高,阿福打算今晚洗劫一条街上的店铺. 这条街上一共有 N 家店铺,每家店中都有一些现金. ...

  8. POP3、IMAP、SMTP邮件协议的理解

    一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 CSDN博客链接: http://blog.csdn.net/my_confesser    正文   今天入职配置OutLook的时候,看到公司的 ...

  9. cocoapods更新

    使用sudo gem install cocoapods更新提示: ERROR: While executing gem ... (Errno::EPERM) Operation not permit ...

  10. java-继承-类变量与实例变量

    父类中的类的变量(静态属性)与其子类共享一份. 父类中的实例变量与其子类各自维护各自的.