ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description
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
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
words in the dictionary match the corresponding number sequence, each
integer per line.
Sample Input
Sample Output
这个题目大意是问给定的按键序列,能在字典里面找到多少个匹配的。
当然首先需要建立一个字母到按键的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)的更多相关文章
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- HDU 4287 Intelligent IME(map运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- HDU 4287 Intelligent IME(字典树)
在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 4287 Intelligent IME
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
随机推荐
- BC 1.2 模式(Battery Charging Specification 1.2)
转自:http://blog.csdn.net/liglei 转自:http://blog.csdn.net/liglei/article/details/22852755 USB BC1.2有以下三 ...
- jQuery 标签切换----之选项卡的实现
这一次,我自己写了代码,先看html部分: <div class="tab"> <div class="tab_menu"> <u ...
- java 中的final
在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个 ...
- 一、Silverlight中使用MVVM(一)——基础
如果你不知道MVVM模式,我建议你先了解一下MVVM模式,至少要知道实现该模式的意图是什么. 那么我主要通过我认为是已经算是比较简单的例子进行讲解这个模式,当然后面我们会在这个例子的基础上一步一步的进 ...
- python学习(十一)函数、作用域、参数
定义和调用函数 在这里函数的定义和调用和一般的语句没什么不一样,感觉函数也是对象 #!/usr/bin/python def times(x, y): # 定义函数 ...
- Spark源码分析之一:Job提交运行总流程概述
Spark是一个基于内存的分布式计算框架,运行在其上的应用程序,按照Action被划分为一个个Job,而Job提交运行的总流程,大致分为两个阶段: 1.Stage划分与提交 (1)Job按照RDD之间 ...
- 多媒体开发之---live555的多线程支持,原本只是单线程,单通道
1)我对Live555进行了一次封装,但是Live555 是单线程的,里面定义的全局变量太多,我封装好dll库后,在客户端调用,因为多个对话框中要使用码流,我就定义了多个对象从设备端接收码流,建立多个 ...
- Spring、Hibernate 数据不能插入到数据库问题解决
1.问题:在使用Spring.Hibernate开发的数据库应用中,发现不管如何,数据都插不到数据库. 可是程序不报错.能查询到,也能插入. 2.分析:Hibernate设置了自己主动提交仍然无论用, ...
- Linux 开启端口,操作防火墙
命令行方式: 添加10002端口方法 1.vi etc/sysconfig/iptables 2. 开放端口命令:-A INPUT -p tcp -m state --state NEW -m ...
- EasyPlayer-RTSP播放器:从底层到上层专注于RTSP播放Windows、Android、iOS RTSP Player
EasyPlayer-RTSP播放器是一套RTSP专用的播放器,包括有:Windows(支持IE插件,npapi插件).Android.iOS三个平台,是由EasyDSS团队开发和维护的区别于市面上大 ...