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. IOS开发-Swift新语言初见

    Safe Swift pairs increased type safety with type inference, restricts direct access to pointers, and ...

  2. 使用 COM 风格的编程接口

    使用COM 风格的编程接口 假设不直接使用 COM 库.不创建自己的包装.那么更可能的是使用 COM 风格的编程接口.这是由于如今很多开发商公布应用程序时.提供了首选的互操作程序集(Primary I ...

  3. 记一次内存泄漏DUMP分析

    自从进入一家创业公司以后,逐渐忙成狗,却无所收获,感觉自身的技术能力用武之地很少,工作生活都在业务逻辑中颠倒. 前些天线上服务内存吃紧,让运维把DUMP拿下来,分析一下聊以自慰. 先来统计一下大对象信 ...

  4. AR9331中Linux内核启动中与IRQ中断相关的文件

    先列出框架,具体后继再来分析. 首先是lds文件,该文件设置了各个section在FLASH或RAM中的先后顺序. 位于~/openwrt1407/build_dir/target-mips_34kc ...

  5. 安装Windows8.1操作系统 - 初学者系列 - 学习者系列文章

    Windows 8这款操作系统是微软最新的操作系统.虽然微软做了推广,但是据消息称市场份额暂时没那么高.下面就对该操作系统的安装进行简要介绍. 1.  将光盘装入光驱,设置BIOS中光驱启动,启动计算 ...

  6. CentOS 5.8安装SugarCRM 6.5版本

    环境:CentOS 5.8,安装了Asterisk 1.8 升级php到5.2SugarCRM 6.5:  Minimum PHP version required is 5.2.0. You are ...

  7. Xamarin Mono 环境搭建

    Xamarin Mono 环境搭建(使用Visual Studio 2013 开发android 和 ios ) 本文主要介绍Xamarin结合VS2013来开发Android应用程序,主要会介绍Mo ...

  8. jQuery 1.9 移除了 $.browser 的替代方法

    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...

  9. NServiceBus 入门2

    NServiceBus官方文档翻译(二)NServiceBus 入门   在这篇教程中我们将学习如何创建一个非常简单的由客户端向服务端发送消息的订单系统.该系统包括三个项目:Client.Server ...

  10. 微软 PowerShell Script Explorer

    微软 PowerShell Script Explorer 满血复活,正式发布 一年前的今天,微软在其Windows PowerShell官方博客声明中止 ‘Script Explorer’ 应用程序 ...