[字典树] HDU 4287 - Intelligent IME
来源:互联网 发布:兽娘动物园 知乎 编辑:程序博客网 时间:2024/04/27 17:37
题意:
给定每个数字对应的几个字母,然后计算输入每串数字,可能输出几种对应的字母?
这题一开始我把字母加入到字典树里了,然后dfs枚举数字可能对应的字母,然后查询~
后来想了一下,发现把数字加入到字典树复杂度降低很多,然后用一个一维数组把每个字母对应的数字存起来。进行查询即可。。
#include <stdio.h>#include <string.h>int f[1000]={2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};struct Trie{ Trie *ch[10]; bool flag; int sz;};Trie *H;int idx(char c){ return c - '0';}Trie *newnode(){ Trie *p = new Trie; for(int i = 0; i < 10; i++) { p->ch[i] = NULL; } p->flag = false; p->sz = 0; return p;}void insert(char *ss){ int len = strlen(ss); Trie *p = H; for(int i = 0; i < len; i++) { int c = idx(ss[i]); if(p->ch[c] == NULL) { p->ch[c] = newnode(); } p = p->ch[c]; } p->flag = true;}int query(char *ss, int x){ Trie *p = H; int len = strlen(ss); for(int i = 0; i < len; i++) { int c = idx(ss[i]); p = p->ch[c]; if(p == NULL) { return 0; } } if(x && p->flag) { p->sz++; } return p->sz;}int main(){ char c[10], a[5000][10]; int T, N, M; scanf("%d", &T); while(T--) { H = newnode(); scanf("%d %d", &N, &M); for(int i = 0; i < N; i++) { scanf("%s", a[i]); insert(a[i]); } for(int i = 0; i < M; i++) { scanf("%s", c); int len = strlen(c); for(int j = 0; j < len; j++) { c[j] = f[c[j]-'a']+'0'; } //printf("query%s\n", c); query(c, 1); } for(int i = 0; i < N; i++) { printf("%d\n",query(a[i], 0)); } } return 0;}
0 0
- Hdu 4287 Intelligent IME 字典树 Hash
- hdu 4287 Intelligent IME (字典树)
- [字典树] HDU 4287 - Intelligent IME
- hdu 4287 Intelligent IME (字典树)
- HDU 4287 Intelligent IME (字典树 && map)
- hdu 4287 Intelligent IME 字典树
- HDU - 4287 Intelligent IME 字典树
- HDU 4287 Intelligent IME(字典树)
- HDU 4287 Intelligent IME(字典树)
- HDU-4287 Intelligent IME (Trie 字典树 入门题)
- hdoj 4287 Intelligent IME 字典树
- hdu 4287 Intelligent IME
- HDU-4287-Intelligent IME
- HDU 4287 Intelligent IME
- hdu 4287 Intelligent IME
- Intelligent IME hdu 4287
- HDU 4287 Intelligent IME
- HDU 4287 Intelligent IME
- 打印org.eclipse.xsd.XSDSchema对象
- http系列一
- 简单的事件机制
- java学习——数组
- 软件质量之道:PCLint之二
- [字典树] HDU 4287 - Intelligent IME
- 基于maven的web项目构建(1)
- CSS:不可思议的border属性
- 判断scanf接收的数据是否为一组纯数字
- 使用StretchBlt函数进行图像缩放
- Spring3.0的AOP基于Annotation AOP基于XML
- Cocos2d-x之2048
- [缘娜衣]2014冬季新款韩版军工装 短款羽绒服 羽绒棉服 棉衣短款
- POJ 2828 Buy Ticketshttp://poj.org/problem?id=2828