Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1279    Accepted Submission(s): 669

Problem 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,用字典树时注意将字母字符串转换为数字字符串进行构建,不要直接构建原串,然后将数字字符串转换为字母字符串进行查找,这样会超时

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=10;
const int N=5000+10;
char s[N][8],ch[8];
char d[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int sum; struct TrieNode{
int num;
TrieNode *next[MAX];
TrieNode(){
num=0;
memset(next,0,sizeof next);
}
}root; void InsertNode(char *a){
int k=0;
TrieNode *p=&root;
while(a[k]){
if(!p->next[a[k]-'0'])p->next[a[k]-'0']=new TrieNode;
p=p->next[a[k++]-'0'];
++p->num;
}
} int SearchTrie(char *a){
int k=0;
TrieNode *p=&root;
while(a[k] && p->next[a[k]-'0'])p=p->next[a[k++]-'0'];
if(a[k])return 0;
return p->num;
} void trans(char *a){
int len=strlen(a);
for(int i=0;i<len;++i){
if(a[i] == 'a' || a[i] == 'b' || a[i] == 'c')a[i]='2';
else if(a[i] == 'd' || a[i] == 'e' || a[i] == 'f')a[i]='3';
else if(a[i] == 'g' || a[i] == 'h' || a[i] == 'i')a[i]='4';
else if(a[i] == 'j' || a[i] == 'k' || a[i] == 'l')a[i]='5';
else if(a[i] == 'm' || a[i] == 'n' || a[i] == 'o')a[i]='6';
else if(a[i] == 'p' || a[i] == 'q' || a[i] == 'r' || a[i] == 's')a[i]='7';
else if(a[i] == 't' || a[i] == 'u' || a[i] == 'v')a[i]='8';
else if(a[i] == 'w' || a[i] == 'x' || a[i] == 'y' || a[i] == 'z')a[i]='9';
else a[i]='1';
}
} void Free(TrieNode *p){
for(int i=0;i<10;++i)if(p->next[i])Free(p->next[i]);
delete p;
} int main(){
int t,n,m;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;++i)scanf("%s",s[i]);
for(int i=0;i<m;++i){
scanf("%s",ch);
trans(ch);
InsertNode(ch);
}
for(int i=0;i<n;++i)printf("%d\n",SearchTrie(s[i]));
for(int i=0;i<10;++i){
if(root.next[i])Free(root.next[i]);
root.next[i]=0;
}
}
return 0;
}

hdu4284之字典树的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. Cocos2d-x3.0之路--02(引擎文件夹分析和一些细节)

    关于怎么搭建好开发环境的我就不写了,网上非常多. 那么 我们来看看 引擎文件的文件夹 所谓知己知彼 百战不殆嘛 先说一下setup.py 这个文件是有关配置的python文件,比方我们在进行andro ...

  2. js面向对象+一般方法的选项卡

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. location将地址栏参数拆分成键值对的对象

    window.location可获取地址栏的一系列信息,并且每个浏览器都支持该属性,非常方便.而获取到的问号后面的参数可以进行加工转变成我们所想要的键值对. location的属性: 属性名 例子 说 ...

  4. POJ3187 Backward Digit Sums

    给出杨辉三角的顶点值,求底边各个数的值.直接DFS就好了 #include<iostream> #include<cstdio> #include<cstring> ...

  5. LinQ—扩展方法

    概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...

  6. 异步陷阱之IO

    异步陷阱之IO篇 很多教程和资料都强调流畅的用户体验需要异步来辅助,核心思想就是保证用户前端的交互永远有最高的优先级,让一切费时的逻辑通通放到后台,等到诸事完备,通知一下前端给个提示或者继续下一步.随 ...

  7. SuspendLayout()了解方法

     SuspendLayout()暂时挂起的布局逻辑控制(msdn),它ResumeLayout()在会同.我的理解是,使用SuspendLayout()让整个窗体站,等到所有的东西都设置齐全,然后Re ...

  8. ADFS 2.0 配置简介 PartⅢ – 声明规则语言

    上一篇我们最终把 ADFS 与应用之间的信任关系建立起来了,但是应用接收到的声明信息只有默认的两个,这次我们就来学学怎么配置声明. 一.声明存储配置 ADFS 目前默认支持三种方式的声明值存储,另外还 ...

  9. Windows上memcached的使用

    Memcached是什么?Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. Memcached能缓存什 ...

  10. LinQ动态排序

    LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为sort这个名称填写错误,会导致后台一直无法获取datagrid的排序字段 本来是没有这一讲的,为了使2 ...