算法:字典树

题意:手机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的更多相关文章

  1. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4287 Intelligent IME hash

    Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  3. HDU 4287 Intelligent IME(map运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 Intellig ...

  4. HDU 4287 Intelligent IME

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 4287Intelligent IME(简单hash)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4287-Intelligent IME(哈希)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  8. HDU 4287 Intelligent IME(string,map,stl,make_pair)

    题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...

  9. HDU 4287 Intelligent IME(字典树)

    在我没用hash之前,一直TLE,字符串处理时间过长,用了hash之后一直CE,(请看下图)我自从经历我的字典树G++MLE,C++AC以后,一直天真的用C++,后来的CE就是因为这个,G++才支持这 ...

随机推荐

  1. RMQ问题

    关于RMQ的问题我就直接截取刘汝佳的<算法竞赛训练指南>上的解释了

  2. [转载]C++ 堆与栈简单的介绍

    在C和C++中,有三种使用存储区的基本方式: [静态存储区(Static   Memory)] 在静态存储区中,连接器(linker)根据程序的需求为对象分配空间.全局变量.静态类成员以及函数中的静态 ...

  3. mysql 5.6 参数详解

    系统变量提供的是各种与服务器配置和功能有关的信息.大部分的系统变量都可以在服务器启动时进行设置.在运行时,每一个系统变量都拥有一个全局值或会话值,或者同时拥有这两个值.许多系统变量都是动态的,也就是说 ...

  4. HIVE快速入门

    (一)简单入门 1.创建一个表 create table if not exists ljh_emp( name string, salary float, gender string) commen ...

  5. linux 下使用crontab+wget实现秒及定时任务

    输入命令 crontab -e 打开一个文件,默认的编辑器为vi. 输入vi编辑器,输入i为插入,输入w保存,q退出,!强制.wq!强制保存并退出. * * * * * /usr/bin/wget - ...

  6. 给大家推荐几款OSX上非常好工具(针对程序员)

    前两天,在App Store无意中发现几款免费工具,感觉非常好用,推荐给大家,希望大家喜欢. 一个是帮助文档管理软件,Dash.以前在Windows上开发,经常要自己搜集一些手册,文档,什么html手 ...

  7. linux 下进程状态及进程控制

    系统状态检测及进程控制1,/proc 是系统的一个窗户,可以透视内核2,建议将hosts里localhost,locahost.locadomain 解析为127.0.0.1 把系统域名解决为局域网的 ...

  8. MySQL flush tables with read lock

    mysql> flush tables with read lock; flush tables with read lock 会去关闭已经打开的所有文件,它要做这个操作就先要拿到锁:当发起这个 ...

  9. HSV颜色识别demo

    HSV(Hue, Saturation, Value)色彩空间是一种区别与RGB的表示形式.其模型可视为一个倒立的棱锥或圆锥. 其中H为色调,用角度度量,取值范围为0°-360°,从红色开始按逆时针方 ...

  10. 手动替换GCC版本

    当我们的系统里面会有2个以上版本的gcc时,系统会缺省的默认一个gcc版本,当然我们可以更改系统的默认配置,来降低gcc.g++的版本以满足不同的需求. 1.查看GCC的版本信息 $ls /usr/b ...