T9

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2747    Accepted Submission(s): 1029

Problem Description
A while ago it was quite cumbersome to create a message for the Short Message Service (SMS) on a mobile phone. This was because you only have nine keys and the alphabet has more than nine letters, so most characters could only be entered by pressing one key several times. For example, if you wanted to type "hello" you had to press key 4 twice, key 3 twice, key 5 three times, again key 5 three times, and finally key 6 three times. This procedure is very tedious and keeps many people from using the Short Message Service.

This led manufacturers of mobile phones to try and find an easier way to enter text on a mobile phone. The solution they developed is called T9 text input. The "9" in the name means that you can enter almost arbitrary words with just nine keys and without pressing them more than once per character. The idea of the solution is that you simply start typing the keys without repetition, and the software uses a built-in dictionary to look for the "most probable" word matching the input. For example, to enter "hello" you simply press keys 4, 3, 5, 5, and 6 once. Of course, this could also be the input for the word "gdjjm", but since this is no sensible English word, it can safely be ignored. By ruling out all other "improbable" solutions and only taking proper English words into account, this method can speed up writing of short messages considerably. Of course, if the word is not in the dictionary (like a name) then it has to be typed in manually using key repetition again.


Figure 8: The Number-keys of a mobile phone.

More precisely, with every character typed, the phone will show the most probable combination of characters it has found up to that point. Let us assume that the phone knows about the words "idea" and "hello", with "idea" occurring more often. Pressing the keys 4, 3, 5, 5, and 6, one after the other, the phone offers you "i", "id", then switches to "hel", "hell", and finally shows "hello".

Write an implementation of the T9 text input which offers the most probable character combination after every keystroke. The probability of a character combination is defined to be the sum of the probabilities of all words in the dictionary that begin with this character combination. For example, if the dictionary contains three words "hell", "hello", and "hellfire", the probability of the character combination "hell" is the sum of the probabilities of these words. If some combinations have the same probability, your program is to select the first one in alphabetic order. The user should also be able to type the beginning of words. For example, if the word "hello" is in the dictionary, the user can also enter the word "he" by pressing the keys 4 and 3 even if this word is not listed in the dictionary.

 
Input
The first line contains the number of scenarios.

Each scenario begins with a line containing the number w of distinct words in the dictionary (0<=w<=1000). These words are given in the next w lines. (They are not guaranteed in ascending alphabetic order, although it's a dictionary.) Every line starts with the word which is a sequence of lowercase letters from the alphabet without whitespace, followed by a space and an integer p, 1<=p<=100, representing the probability of that word. No word will contain more than 100 letters.

Following the dictionary, there is a line containing a single integer m. Next follow m lines, each consisting of a sequence of at most 100 decimal digits 2-9, followed by a single 1 meaning "next word".

 
Output
The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1.

For every number sequence s of the scenario, print one line for every keystroke stored in s, except for the 1 at the end. In this line, print the most probable word prefix defined by the probabilities in the dictionary and the T9 selection rules explained above. Whenever none of the words in the dictionary match the given number sequence, print "MANUALLY" instead of a prefix.

Terminate the output for every number sequence with a blank line, and print an additional blank line at the end of every scenario.

 
Sample Input
2
5
hell 3
hello 4
idea 8
next 8
super 3
2
435561
43321
7
another 5
contest 6
follow 3
give 13
integer 6
new 14
program 4
5
77647261
6391
4681
26684371
77771
 
Sample Output
Scenario #1:
i
id
hel
hell
hello
i
id
ide
idea
Scenario #2:
p
pr
pro
prog
progr
progra
program
n
ne
new
g
in
int
c
co
con
cont
anoth
anothe
another
p
pr
MANUALLY
MANUALLY
 
Source
 题意:
九键的手机键盘如图,给出n个单词以及该单词的频率,然后给出m个由数字组成的字符串,问在按这个字符串过程中依次出现什么单词,(频率大的优先出现,频率可加),如:he 2 hell 3,h的频率就是5。
代码:
//字典树,重点是查找,每个数字键代表多个字母,用dfs找出所有的可能组合,从这些组合中找出存在于
//字典中并且概率最大的。dfs时有一个重要的剪枝。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAX=;
int const CON=;
int nod[MAX][CON],val[MAX];
int sz;
char tel[][]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//0~9按键代表的字母
int te[]={,,,,,,,,,};//0~9按键分别代表几个字母
int t,n,m,p;
string ch1;
void init()
{
sz=;
memset(nod[],,sizeof(nod[]));
val[]=;
}
void insert(string s,int k)
{
int len=s.size();
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'a';
if(nod[rt][id]==)
{
memset(nod[sz],,sizeof(nod[sz]));
nod[rt][id]=sz;
val[sz++]=;
}
rt=nod[rt][id];
val[rt]+=k;
}
}
int search(string s)
{
int len=s.size();
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'a';
if(nod[rt][id]==)
return ;
rt=nod[rt][id];
}
return val[rt];
}
void dfs(int x,int y,int rt,string s,string ch2)
{
if(x>y){
int pp=search(ch2);
if(pp>p){
p=pp;
ch1=ch2;
}
}
else{
int nu=s[x]-'';
for(int i=;i<te[nu];i++){
char tmp=tel[nu][i];
if(nod[rt][tmp-'a'])//判断tmp在不在这条树枝上,一定要有这个判断,不然会超时
dfs(x+,y,nod[rt][tmp-'a'],s,ch2+tmp);
}
}
}
int main()
{
string s;
int k;
scanf("%d",&t);
for(int h=;h<=t;h++){
init(); //静态字典树记住初始化
printf("Scenario #%d:\n",h);
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>s>>k;
insert(s,k);
}
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>s;
int len=s.size();
for(int j=;j<len-;j++){
p=;
ch1="";
dfs(,j,,s,"");
if(p!=)
cout<<ch1<<endl;
else printf("MANUALLY\n");
}
printf("\n");
}
printf("\n");
}
return ;
}

HDU1298 字典树+dfs的更多相关文章

  1. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  2. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  3. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  4. hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs

    题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...

  5. HDU 1298 T9 字典树+DFS

    必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...

  6. hdu 1298 字典树 + DFS (模拟T9文本输入)

    题意:       给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...

  7. hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)

    Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...

  8. LightOJ DNA Prefix(字典树+dfs)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...

  9. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

随机推荐

  1. 小程序开发中,纯css实现内容收起折叠功能

    不多说,直接上代码: wxml页面: <!--收起折叠 begin--> <view style='width:100%;background:#fff;border-top:1px ...

  2. 校招小白机考入坑之从键盘输入java的各种数据类型

    //1.从键盘输入一个整型(其他基本类型类似) Scanner sc =new Scanner(System.in); sc.hasNextInt(); int str1 = sc.nextInt() ...

  3. tensorflow模型持久化保存和加载--深度学习-神经网络

    模型文件的保存 tensorflow将模型保持到本地会生成4个文件: meta文件:保存了网络的图结构,包含变量.op.集合等信息 ckpt文件: 二进制文件,保存了网络中所有权重.偏置等变量数值,分 ...

  4. 【Coursera-ML-Notes】线性回归(下)

    模型表示 多变量的线性回归也叫做"多元线性回归".首先还是先明确几个符号的含义. \(x{^{(i)}_j}\):第i个训练样本的第j个特征,比如面积,楼层,客厅数 \(x^{(i ...

  5. Fluent Python: @property

    Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...

  6. 基础数据类型-tuple

    Python中,元组tuple与list类似,不同之处在于tuple的元素不能修改,tuple使用(),list使用[], (1)元组的创建使用(),需要注意的是创建包含一个元素的元组: tuple_ ...

  7. 【Linux】- CentOS搭建FTP服务器

    1.安装vsftpd yum install -y vsftpd 2.启动vsftpd服务 service vsftpd start 3.查看运行状态 netstat -nltp | 完毕!!! 参考 ...

  8. Matlab 中的varargin/nargin varargout/nargout

    Varargin = var+ arg+ in = variable length(可变长) input argument(输入参数) list(列表) :允许调用该函数时根据需要改变输入参数的个数 ...

  9. Maven面试宝典

    一.Maven有哪些优点和缺点 优点如下: 简化了项目依赖管理: 易于上手,对于新手可能一个"mvn clean package"命令就可能满足他的工作 便于与持续集成工具(jen ...

  10. TCP/IP三次握手与四次握手

    原文地址 http://blog.csdn.net/whuslei/article/details/6667471 http://blog.csdn.net/wo2niliye/article/det ...