HDU1298 字典树+dfs
T9
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2747 Accepted Submission(s): 1029
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.
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".
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.
hello 4
idea 8
next 8
super 3
43321
contest 6
follow 3
give 13
integer 6
new 14
program 4
6391
4681
26684371
77771
id
hel
hell
hello
id
ide
idea
pr
pro
prog
progr
progra
program
ne
new
in
int
co
con
cont
anoth
anothe
another
pr
MANUALLY
MANUALLY
//字典树,重点是查找,每个数字键代表多个字母,用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的更多相关文章
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...
- hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs
题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...
- HDU 1298 T9 字典树+DFS
必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...
- hdu 1298 字典树 + DFS (模拟T9文本输入)
题意: 给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...
- hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)
Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...
- LightOJ DNA Prefix(字典树+dfs)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
随机推荐
- HashMap 阅读
最近研究了一下java中比较常见的map类型,主要有HashMap,HashTable,LinkedHashMap和concurrentHashMap.这几种map有各自的特性和适用场景.使用方法的话 ...
- FPGA学习-PS2接口
选自http://m.elecfans.com/article/774143.html
- 【转载】JAVA常见面试题及解答(精华)
JAVA常见面试题及解答(精华) 1)transient和volatile是java关键字吗?(瞬联) 如果用transient声明一个实例变量,当对象存储时,它的值不需要维持.例如: ...
- [leetcode-738-Monotone Increasing Digits]
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- 衡量经济活动的价值:国内生产总值(GDP, Gross Domestic Product)
定义 GDP是在给定的时期内,经济生产的所有最终产品和服务的市场价值. 由于每一件产品或者服务的交易都会涉及到一个买者和一个卖着,买者支出的每一元钱必然成为卖者收入的每一元钱,因此,GDP既可以看成是 ...
- Alpha发布——视频展示
一.视频链接 http://v.youku.com/v_show/id_XMzEyODQzNzQ2MA==.html 二.视频文案说明 你是不是还在为软工作业奋笔疾书? 你是不是无法及时查看最新博客信 ...
- tomcat端口号修改
修改Tomcat的端口号: 在默认情况下,tomcat的端口是8080,如果出现8080端口号冲突,用如下方法可以修改Tomcat的端口号: 首先: 在Tomcat的根(安装)目录下,有一个conf文 ...
- iOS- iPad里有趣的UIPopoverController
效果: 1.对UIPopoverController的简单概述 1.1 UIPopoverController是在iPad开发中常用的一个组件(在iPhone上不允许使用),使用非常简单 1.2 ...
- WE团队团队汇总
WE团队目录 一.博客汇总 团队展示 选题报告 二.文档汇总 选题报告
- python 查看趴下来的数据
#coding=utf-8 import re from lxml import etree import requests def requests_view(response): import w ...