hdu4284之字典树
Intelligent IME
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1279 Accepted Submission(s): 669
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?
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.
3 5
46
64448
74
go
in
night
might
gn
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之字典树的更多相关文章
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- 各种oracle参数查询语句
各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...
- 网页动态切换母版页(MasterPage)
原文:网页动态切换母版页(MasterPage) 是否可以变更网页的母版页(MasterPage)呢?某.aspx在创建时,已经附加入某一母版页(MasterPage)了,现需要.aspx动态变更母版 ...
- java通过SVG导出图片
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import ja ...
- selenium之多线程启动grid分布式测试框架封装(二)
五.domain类创建 在domain包中创建类:RemoteLanchInfo.java 用来保存启动信息. package com.lingfeng.domain; public class Re ...
- 如何设置Installshield中 feature的选中状态
原文:如何设置Installshield中 feature的选中状态 上一篇: 使用strtuts2的iterator标签循环输出二维数组之前一直有筒子问如何设置Installshield中 feat ...
- php利用SoapClient调用webservices
原文:php利用SoapClient调用webservices 1.开启soap支持,在php.ini中去除extension=php_soap.dll之前的':' 2.掉用页面 <?php h ...
- 【硬件】DELLserver硬件监控和DELL系统管理工具OMSA介绍
1.1.1. DELLserver硬件监控和DELL系统管理工具OMSA介绍 本文介绍了利用使用Nagios和OMSA显示器DELLserver硬件健康状况,Nagios监控的方式是NRPE模式,须要 ...
- 批处理中set截取字符具体解释
set截取字符具体解释 在批处理中,set的功能有点繁杂:设置变量.显示环境变量的名及值.做算术运算.等待用户的输入.字符串截取.替换字符串,是我们经常使用的命令之中的一个. 在字符串截取方面,新手 ...
- Grunt使用入门
Grunt使用入门 (by vczero) 一.前言 项目中一直在使用Grunt,只是对Grunt的基本使用,却未系统的总结过.为什么要构建工具?一句话:自动化.对于需要反复重复的任务,例如压缩(mi ...
- zepto.js的基本介绍与使用
最近看到了一篇文章,是介绍一种新的js框架,名为zepto.js,他适用于移动设备已经桌面浏览器除了ie系列的.. 他兼容jquery的API,所以学起来或用起来并不吃力.他比jquery的优势在于1 ...