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

这个题目大意是问给定的按键序列,能在字典里面找到多少个匹配的。

当然首先需要建立一个字母到按键的Hash,可以使用map也可以使用数组,因为字母的ASCII值不是很大。

这样就能把字典里面的字母序列映射成数字序列了。

然后就是对查询的序列和字典里面对应的Hash序列进行匹配。

这个匹配可以依旧使用map进行映射,map的存入的是序列在字典里面出现的次数。效率是N*logM。

此外,由于是数字的匹配,同样可以看作是字符串匹配,自然可以使用字典树。效率是N*strlen(str),其中strlen指的是平均查询的字符串长度。

可见当M很大时,字典树要快一点。

当然在使用字典树的时候最好释放掉内存,不然内存泄漏,内存消耗很大。

代码:

map版:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; char hashStr[][] = {
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
map<char, int> Hash;
int n, m;
map<int, int> dic;
int query[]; void init()
{
for (int i = ; i < ; ++i)
{
int len = strlen(hashStr[i]);
for (int j = ; j < len; ++j)
Hash[hashStr[i][j]] = i+;
}
} int turn(char str[])
{
int num = , len = strlen(str);
for (int i = ; i < len; ++i)
num = *num + Hash[str[i]];
return num;
} void input()
{
dic.clear();
char str[];
scanf("%d%d", &n, &m);
for (int i = ; i < n; ++i)
scanf("%d", &query[i]);
for (int i = ; i < m; ++i)
{
scanf("%s", str);
dic[turn(str)]++;
}
} void work()
{
for (int i = ; i < n; ++i)
{
if (dic[query[i]])
printf("%d\n", dic[query[i]]);
else
printf("0\n");
}
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

字典树版:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#define LL long long using namespace std; char hashStr[][] = {
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
map<char, int> Hash;
int n, m;
char query[][]; //字典树
struct Tree
{
int num;
Tree *next[];
} *head; void add(char str[])
{
Tree *p = head;
int k, len = strlen(str);
for (int i = ; i < len; ++i)
{
k = str[i]-'';
if (p->next[k] == NULL)
{
Tree *q = (Tree *)malloc(sizeof(Tree));
q->num = ;
for (int i = ; i <= ; ++i)
q->next[i] = NULL;
p->next[k] = q;
}
p = p->next[k];
}
p->num++;
} int Query(char str[])
{
Tree *p = head;
int k, len = strlen(str);
for (int i = ; i < len; ++i)
{
k = str[i]-'';
if (p->next[k] == NULL)
return ;
p = p->next[k];
}
return p->num;
} void del(Tree *p)
{
for (int i = ; i <= ; ++i)
{
if (p->next[i] != NULL)
del(p->next[i]);
}
free(p);
} void init()
{
for (int i = ; i < ; ++i)
{
int len = strlen(hashStr[i]);
for (int j = ; j < len; ++j)
Hash[hashStr[i][j]] = i+;
}
} void turn(char str[])
{
int num = , len = strlen(str);
for (int i = ; i < len; ++i)
str[i] = Hash[str[i]]+''; } void input()
{
head = (Tree *)malloc(sizeof(Tree));
for (int i = ; i <= ; ++i)
head->next[i] = NULL;
char str[];
scanf("%d%d", &n, &m);
for (int i = ; i < n; ++i)
scanf("%s", query[i]);
for (int i = ; i < m; ++i)
{
scanf("%s", str);
turn(str);
add(str);
}
} void work()
{
for (int i = ; i < n; ++i)
printf("%d\n", Query(query[i]));
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
del(head);
}
return ;
}

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)的更多相关文章

  1. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  2. HDU 4287 Intelligent IME(map运用)

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

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

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

  4. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  5. HDU 4287 Intelligent IME(字典树)

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

  6. HDU 4287 Intelligent IME hash

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

  7. HDU 4287 Intelligent IME

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

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

随机推荐

  1. jdk并发工具包之锁

    1.cynchronized扩展:可重锁入ReentrantLock ReentrantLock是通过cas算法实现的 RenntrantLock lock=new ReentrantLock(); ...

  2. 在安装ubuntu时,卡在启动画面

    在我安装ubuntu时发生的情况,记录下来希望能帮助到需要帮助的朋友. 我先后尝试16.04与14.05两个版本的Ubuntu系统,方法均为:https://www.ubuntu.com/downlo ...

  3. 通用安防摄像机通过RTSP转RTMP推流进行H5(RTMP/HLS)直播的方案

    EasyNVR摄像机无插件直播方案 随着互联网的发展,尤其是移动互联网的普及,基于H5.微信的应用越来越多,企业也更多地想基于H5.微信公众号来快速开发和运营自己的视频及视频相关性产品,那么传统的安防 ...

  4. c++对象内存的分配

    1 关于c++的对象 只要是用了class或者struct定义的,都是对象,不管有没有方法.不过,一般情况下,没有方法的对象用struct关键字来定义. 2 不用new关键字定义对象 要看这样的对象在 ...

  5. ZookeeperclientAPI之创建会话(六)

    Zookeeper对外提供了一套Java的clientAPI. 本篇博客主要讲一下创建会话. 创建项目 首选,创建一个基于maven管理的简单javaproject.在pom文件里引入zookeepe ...

  6. swift中反向循环

    First of all, protocol extensions change how reverse is used: for i in (1...5).reverse() { print(i) ...

  7. CSS3定时提示动画特效

    在线演示 本地下载

  8. qemu仿真执行uboot和barebox

    先安装qemu: apt-get install qemu-system 交叉编译器可以选择友善之臂:http://arm9download.cncncn.com/mini2440/linux/arm ...

  9. IIS 7.5 虚拟主机独立用户的配置.

    1:新建用户 2:打开 IIS->功能视图->打开编辑身份验证->匿名身份验证,点右边编辑->匿名用户标识中选"特定用户"->确定. 3:编缉网站的权 ...

  10. 自定义编辑框VC,可加载更改字体,添加背景图片,显示输入提示信息

    搞了一天终于弄了个完整的编辑框控件出来了, 哎,,,搞界面开发还是有点复杂的. #pragma once #include "AdvEdit.h" // CBkgEditBox c ...