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博弈,必败局势是所 ...
随机推荐
- java利用爬虫技术抓取(省、市(区号\邮编)、县)数据
近期项目须要用到 城市的地址信息,但从网上下载的xml数据没有几个是最新的地址信息.....数据太老,导致有些地区不全.所以才想到天气预报官网特定有最新最全的数据.贴出代码,希望能给有相同困惑的朋友. ...
- matlab 学习之常用函数2
-----------------------------author:midu ---------------------------qq:1327706646 ------------------ ...
- iPhone,iPad如何获取WIFI名称即SSID
本文转载至 http://blog.csdn.net/wbw1985/article/details/20530281 2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉 ...
- Git 自己的一些工作中的总结
这个网址很重要:https://gitee.com/progit/2-Git-%E5%9F%BA%E7%A1%80.html#2.4-%E6%92%A4%E6%B6%88%E6%93%8D%E4%BD ...
- 高性能流媒体服务器EasyDSS前端重构(二) webpack + vue + AdminLTE 多页面提取共用文件, 优化编译时间
本文围绕着实现EasyDSS高性能流媒体服务器的前端框架来展开的,具体EasyDSS的相关信息可在:www.easydss.com 找到! 接上回 <高性能流媒体服务器EasyDSS前端重构(一 ...
- Git you are not allowed to push code to protected branches on this project?
error: You are not allowed to push code to protected branches on this project....error: failed to pu ...
- Android Development Note-01
Eclipse快捷键: 导包:ctrl+alt+o 格式化代码:ctrl+alt+f MVC: M——Model V——View C——Control android程序界面如何设计.调试 U ...
- Mysql 外键级联
如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Other Linker flags 添加 -Objc导致包冲突
Other Linker flags 添加 -Objc导致包冲突 先尝试不添加-Objc,不行的话尝试下面的方法. 第三方冲突解决办法: https://www.jianshu.com/p/02846 ...