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博弈,必败局势是所 ...
随机推荐
- python学习(九)python中的变量、引用和对象的关系
<Think In Java>中说到过"万事万物皆对象",这句话也可以用在Python中. 感觉Python中的变量有点像Javascript中的变量,是弱类型的,但是 ...
- 将web工程部署到tomcat
http://blog.csdn.net/lucklq/article/details/7621807 http://jingyan.baidu.com/article/466506582f2f4af ...
- 具体解释TCP协议的服务特点以及连接建立与终止的过程(俗称三次握手四次挥手)
转载请附本文的链接地址:http://blog.csdn.net/sahadev_/article/details/50780825 ,谢谢. tcp/ip技术经常会在我们面试的时候出现,非常多公司也 ...
- 继续封装DBDA.php 加入ajax
<?php class DBDA { public $host = "localhost"; //服务器地址 public $uid = "root"; ...
- EasyNVR流媒体服务器网页兼容调试:ie浏览器下的接口调用成功但页面无法显示实时的数据
许多问题很难在开发的过程中就想的面面俱到,都是在实际应用.调试的过程中一一的优化的.由于easynvr的受众越来越多,因此也出现了好多在开发之初并没有留意的一些细节.我这次发现的问题就是给客户远程解决 ...
- The connection between feature spaces and smoothness is not obvious, and is one of the things we’ll discuss in the course.
http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/lecture4_introToRKHS.pdf
- Leslie Lamport
http://lamport.azurewebsites.net/pubs/pubs.html paper
- json (js对象标记)
基础 JSON: JavaScript Object Notation (JavaScript对象表示法) 网络媒体类型是 application/json,文件名扩展是 .json JSON 独立于 ...
- php 身份证号码获取星座和生肖
发布:thatboy 来源:Net [大 中 小] 本文介绍下,php用身份证号码获取星座和生肖的方法,一个简单的php实例,从身份证号码中取得星座与生肖信息,有兴趣的朋友参考研究下吧.本 ...
- 模型层的Meta选项详解
一 . 模型层的Meta选项详解 Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.便用方法及参数解释如下 : class Book(models.Model): ...