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. 将Render博客搬至GIT(偷懒)

    SmallEngine 一个特别小的研究引擎[用于各种实验] 框架上设计上采用Unreal.Unity的设计思路[偷懒了] https://github.com/daozhangXDZ/DZSmall ...

  2. 在使用Pipeline串联多个stage时model和非model的区别

    train.csv数据: id,name,age,sex1,lyy,20,F2,rdd,20,M3,nyc,18,M4,mzy,10,M 数据读取: SparkSession spark = Spar ...

  3. visionpro9.0破解

    visionpro9.0软件下载 提供一个visionpro9.0视频教程学习网站:点击下面链接进入. ------------------------Halcon,Visionpro高清视频教程,点 ...

  4. ServiceStack.Ormlit 事务

    应该使用这个方法开启事务 public static IDbTransaction OpenTransaction(this IDbConnection dbConn) { return new Or ...

  5. linux下的常用技巧。

    xargs  linux下的多行合并~ [root@]# yum list installed|grep php|awk -F ' ' '{print $1}' php-channel-nrk.noa ...

  6. Mysql 工作原理

    刚开始接触一个新的事物的时候,我觉得很有必要从其工作原理入手,弄清楚这个东西的来龙去脉,为接下来的继续深入学习做好铺垫,掌握好其原理有助于我们从整体上来把握这个东西,并且帮助我们在排错过程中理清思路. ...

  7. Java中ArrayList与数组间相互转换

    在实际的 Java 开发中,如何选择数据结构是一个非常重要的问题. 衡量标准化(读的效率与改的效率) : ① Array: 读快改慢 ② Linked :改快读慢 ③ Hash:介于两者之间 实现Li ...

  8. 3DMAX2016安装教程【图文】

    下载安装包之后,双击setup.exe. 下面是安装图片教程: 点击安装 点击下一步. 如图输入序列号和产品密钥. 填写安装路径,然后下一步. 开始安装,等待. 安装成功.

  9. 数字证书认证这点事, SSL/TLS,OpenSSL

    1.概念 数字证书 HTTPS请求时,Server发给浏览器的认证数据,用私钥签名,并且告诉浏览器公钥,利用公钥解密签名,确认Server身份. 证书还会指明相应的CA,CA能确认证书是否真的是CA颁 ...

  10. GetTickCount 和getTickCount

    GetTickCount:正常读取时间函数 getTickCount:不知道是什么鬼东东函数 都包含在windows.h中..运行出的结果天壤之别~~~