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 ...
随机推荐
- C++复合类型(结构,共用体,枚举)
•结构是用户定义的类型,而结构的声明定义了这种类型的数据属性. 一.关键字struct声明: 定义了一种新类型 struct inflatable{ char name[20];//结构成员 fl ...
- tensorflow学习笔记(3)前置数学知识
tensorflow学习笔记(3)前置数学知识 首先是神经元的模型 接下来是激励函数 神经网络的复杂度计算 层数:隐藏层+输出层 总参数=总的w+b 下图为2层 如下图 w为3*4+4个 b为4* ...
- 特殊符号 & 以太坊
&表示取二进制的末尾 &1表示如果末尾是奇数和偶数两种情况 0 偶数 1奇数 举例子: int a=1;int p=&a; 其中,p是指针,&a就是将a在内存中的实际地 ...
- 编译安装hadoop2.6.3
一.安装环境 1.1 JAVA 安装java1.7 下载jdk1.7: [root@node1~]# wget http://download.oracle.com/otn-pub/java/jd ...
- 基础数据类型-dict
字典Dictinary是一种无序可变容器,字典中键与值之间用“:”分隔,而与另一个键值对之间用","分隔,整个字典包含在{}内: dict1 = {key1:value1, key ...
- 如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置
如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置 <div style="position:relative;"> <img src=&quo ...
- lintcode-14-二分查找
二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 在数组 ...
- project之chrome.exe
查看chrome.exe的以来文件可以得到下面这个列面,大部分是在%systemroot%/system32下面的系统dll文件,只有两个是chromium自己生成的:base.dll, conten ...
- oracle 删除数据恢复
select * from taxi_comp_worksheet_ext as of timestamp to_timestamp('2014-09-22 13:00:00', 'yyyy-m ...
- 关于对 NUMA 理解(学习笔记,便于以后查阅)
对NUMA的理解: NUMA是多核心CPU架构中的一种,其全称为Non-Uniform Memory Access,简单来说就是在多核心CPU中,机器的物理内存是分配给各个核的,架构简图如下所示: 每 ...