hdu Intelligent IME
算法:字典树
题意:手机9键拼音:2:abc 3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz;
第一行读入一个T,代表测试组数;
之后输入两个整数n和m,
有n个数字串和m个字符串
让你判断给定的数字串可以得到几种字符组合,在给定的字符串中;并输出相应的个数;
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
思路:用字符串在数字2-9之间建树,查找就方便多了;
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <cstring>
using namespace std;
#define Max 13
struct dot
{
dot *next[Max];
int flag;
} ;
dot *newnode()
{
dot *temp=new dot;
temp->flag=0;
for(int i=0;i<Max;i++)
temp->next[i]=NULL;
return temp;
}
void tree(char *st,dot *root)
{
dot *p=root;
int id=0;
int len=strlen(st);
for(int i=0;i<len;i++)
{
if(st[i]>='a'&&st[i]<='c')
id=2;
else if(st[i]>='d'&&st[i]<='f')
id=3;
else if(st[i]>='g'&&st[i]<='i')
id=4;
else if(st[i]>='j'&&st[i]<='l')
id=5;
else if(st[i]>='m'&&st[i]<='o')
id=6;
else if(st[i]>='p'&&st[i]<='s')
id=7;
else if(st[i]>='t'&&st[i]<='v')
id=8;
else if(st[i]>='w'&&st[i]<='z')
id=9;
if(p->next[id]==NULL)
p->next[id]=newnode();
p=p->next[id];
}
p->flag++;
}
int find(char *st,dot *root)
{
int id;
dot *p=root;
for(int i=0;i<strlen(st);i++)
{
id=st[i]-'0';
if(p->next[id]==NULL)
return 0;
p=p->next[id];
}
return p->flag;
}
void del(dot *t)
{
if(t==NULL) return ;
for(int i=0;i<Max;i++)
if(t->next[i]!=NULL)
del(t->next[i]);
delete t;
}
int main()
{
int n,m,i,j,k,t;
cin>>t;
char s[5005][20],st[30];
while(t--)
{
cin>>n>>m;
dot *root=new dot;
root=newnode();
for(i=0;i<n;i++)
cin>>s[i];
while(m--)
{
cin>>st;
tree(st,root);
}
for(i=0;i<n;i++)
cout<<find(s[i],root)<<endl;
del(root);
}
return 0;
}
hdu Intelligent IME的更多相关文章
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 4287 Intelligent IME(map运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...
- HDU 4287 Intelligent IME
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4287Intelligent IME(简单hash)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4287-Intelligent IME(哈希)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- HDU 4287 Intelligent IME(string,map,stl,make_pair)
题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...
- HDU 4287 Intelligent IME(字典树)
在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...
随机推荐
- javascript事件详细说明
javascript事件列表解说javascript事件列表解说事件 浏览器支持 解说一般事件 onclick IE3.N2 鼠标点击时触发此事件ondblclick IE4.N4 鼠标双击时触发此事 ...
- ubuntu 启用apache2 虚拟机配置
Ubuntu 启用apache2 虚拟机配置 http://jingyan.baidu.com/article/5d6edee20b78e999eadeecf7.html
- xml文件操作
public static XmlDocument getDoc(String path)//加载xml文档 { XmlDocument doc = new XmlDocument(); doc.Lo ...
- iOS 的 APP 在系统中如何适配不同的屏幕的尺寸
iOS 的 APP 在系统中如何适配不同的屏幕的尺寸 标签: 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone ...
- matlab的绘图保存
matlab的绘图和可视化能力是不用多说的,可以说在业内是家喻户晓的.Matlab提供了丰富的绘图函数,比如ez**系类的简易绘图函数,surf.mesh系类的数值绘图函数等几十个.另外其他专业工 ...
- gets和从键盘输入换行符
i was wrong! 虽然setbuf可以让程序自己管理缓冲,但是像getchar,gets这些标准IO函数还是要通过隐藏的stdin进行操作,而stdin是啥呢?还是一个FILE*,而FILE* ...
- smarty 内置函数if 等判断
{if},{elseif},{else} Smarty的{if}条件判断和PHP的if 非常相似,只是增加了一些特性. 每个{if}必须有一个配对的{/if}. 也可以使用{else} 和 {else ...
- QT类的继承结构
QT类的继承结构 QT的类 core 数据集合 QString 几何类 QPoint QSize QRectangle 系统类 QColor QFont QImage QIcon QCursor QB ...
- logstash 安装zabbix插件
<pre name="code" class="html">[root@xxyy yum.repos.d]# yum install ruby Lo ...
- Linux企业级项目实践之网络爬虫(15)——区分文本文件和二进制文件
HTTP协议支持文本和二进制文件传输.最常见的html格式的页面即文本,图片.音乐等为二进制文件.我们要对这两类文件加以区分并分别处理. static char * BIN_SUFFIXES = &q ...