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-x横版ACT手游源 2、server场景

    仍然一样 直接在代码 资源 下一个 上传 你可以看到自己 NFServerChangeLayer.h </pre><pre name="code" class=& ...

  2. iptables的配置文件/etc/sysconfig/iptables不存在怎么办

    iptables的配置文件/etc/sysconfig/iptables不存在怎么办 首先要看一下iptables是否安装了,使用service iptables status或yum info ip ...

  3. CSS3 选择器读解

    文章资料来自于W3Cfuns CSS3.0 四个基本的结构性伪类选择器:root 此选择器将绑定到页面的根元素中,所谓根元素,是指文档树中最顶层的元素,也就是<html>部 分. < ...

  4. Windows WDDM显卡驱动框架及GPUView工具的使用(1)

    也许这个夏天会写一些东西,这里先说说我做过些什么,知道些什么. 过去的五年里,先后在Vista,Win7和Win8上写过显卡驱动,包括umd和kmd的驱动,积累了大量关于WDDM的经验. 我知道WMM ...

  5. 设计模式---订阅发布模式(Subscribe/Publish)

    设计模式---订阅发布模式(Subscribe/Publish) 订阅发布模式定义了一种一对多的依赖关系,让多个订阅者对象同时监听某一个主题对象.这个主题对象在自身状态变化时,会通知所有订阅者对象,使 ...

  6. Phpstorm配置phpunit对php进行单元测试

    在 phpstorm 中配置 php 项目的单元测试,项目使用 Composer 进行管理,为了避免在项目中直接引入 phpunit 相关代码包,使项目的 vendor 目录变得臃肿,这里采用全局安装 ...

  7. MVC5 EF6 Bootstrap3 HtmlHelper

    MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下) 上一节:MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上) 源码下载 ...

  8. 使用c#给outlook添加任务、发送邮件

    原文:使用c#给outlook添加任务.发送邮件 c#在使用outlook提供的一些API时,需要将outlook相关的com引用到项目中. 具体方法就是用vs打开工程后,在工程上添加引用,在com选 ...

  9. android:Fragment动画的东西

    最近很多人来Fragment动画是很感兴趣,我将是一个样本给大家看. 既然做,我会做动画以下类型: 注入弹出动画:从""进入.从"上下左右"弹出,当然,你怎么组 ...

  10. ASP.NET开发,简化与封装

    ASP.NET开发,简化与封装 微软的ASP.NET的开发,就是面向对象的编程,当然前端也能体验至面向对象的话,使用Web控件也必须的. 任一控件,我们均可以在后端.aspx.cs或.aspx.vb程 ...