T9

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 82 Accepted Submission(s): 47
 
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
Northwestern Europe 2001
 
Recommend
JGShining
 
/*
题意:给你一个传统的九宫格键盘,然后给出n个单词并且给出各自的使用频率,现在给出你按键的顺序,让你输出按下哪个键
最有可能出现在屏幕上的字符串。
初步思路:1.字典树将每个前缀都存进树中。
2.然后从树的0号节点开始遍历。
3.每遍历到操作的一个按键,就开始遍历这个按键上的字符作为下一步,看树上是不是有。
4.每次遍历完了都比较一下,是不是概率最高的。 #错误:自己觉得没问题,但是就是WA。插入函数写的有问题
*/
#include<bits/stdc++.h>
using namespace std;
string tmp;
int sum=;
int m[]={,,,,,,,,,};//表示每个按键上的字母数
char key[][]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//表示每个按键上的字母
int t,n,p;
int val;
char str[];
/***************************************************字典树模板***************************************************/
#define MAX 26
const int maxnode=*+;///预计字典树最大节点数目
const int sigma_size=;///每个节点的最多儿子数 struct Trie
{
///这里ch用vector<26元素的数组> ch;实现的话,可以做到动态内存
int ch[maxnode][sigma_size];///ch[i][j]==k表示第i个节点的第j个儿子是节点k
int val[maxnode];///val[i]==x表示第i个节点的权值为x
int sz;///字典树一共有sz个节点,从0到sz-1标号 ///初始化
void Clear()
{
sz=;
memset(ch[],,sizeof(ch[]));///ch值为0表示没有儿子
memset(val,,sizeof val);
} ///返回字符c应该对应的儿子编号
int idx(char c)
{
return c-'a';
} ///在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
///所以执行Insert前需要判断一下是否已经存在s单词了
void Insert(char *s,int v)
{
int u=,n=strlen(s);
for(int i=;i<n;i++)
{
int id=idx(s[i]);
if(ch[u][id]==)//无该儿子
{
ch[u][id]=sz;
memset(ch[sz],,sizeof(ch[sz]));
val[sz++]+=v;//该前缀加上相应的概率
}else
val[ch[u][id]]+=v;
u=ch[u][id];
}
} ///在字典树中查找单词s
void Search(int k,int len,int u,string a)//当前遍历到str的第几个字符,需要遍历的总长度,当前遍历的字符,累加器
{
if(k==len){
if(val[u]>sum){
sum=val[u];
tmp=a;
}
}
int t=str[k]-'';//当前的数字
for(int i=;i<m[t];++i){//遍历这个按键所有的字母
int id=idx(key[t][i]);
if(ch[u][id])//如果下一个字符树上有
Search(k+,len,ch[u][id],a+key[t][i]);
}
}
};
Trie trie;///定义一个字典树
/***************************************************字典树模板***************************************************/
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
for(int ca=;ca<=t;ca++){
printf("Scenario #%d:\n",ca);
trie.Clear();
scanf("%d",&n);
for(int i=;i<n;i++){//加入新的字符串和相应的概率
scanf("%s%d",str,&val);
trie.Insert(str,val);
}
// for(int i=0;i<trie.sz;i++){
// cout<<trie.val[i]<<" ";
// }cout<<endl;
scanf("%d",&p);
for(int i=;i<p;i++){
scanf("%s",&str);//查询的字符串
for(int i=;i<strlen(str)-;i++){
sum=;
trie.Search(,i+,,"");
if(sum) cout<<tmp<<endl;
else puts("MANUALLY");
}
cout<<endl;
}
cout<<endl;
}
return ;
}

T9的更多相关文章

  1. POJ 1451 T9

    T9 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3083   Accepted: 1101 Description Ba ...

  2. http://blog.csdn.net/bluejoe2000/article/details/39521405#t9

    http://blog.csdn.net/bluejoe2000/article/details/39521405#t9

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

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

  4. hdu 1298 T9(特里+DFS)

    pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...

  5. POJ 1451 - T9 - [字典树]

    题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...

  6. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  7. 玩爆你的手机联系人--T9搜索(一)

         自己研究了好几天联系人的T9搜索算法, 先分享出来给大家看看. 欢迎不吝赐教.假设有大神有更好的T9搜索算法, 那更好啊,大家一起研究研究,谢谢. 第一部分是比較简单的获取手机联系人. 获取 ...

  8. 套题T8&T9

    A - 8球胜负(eight) Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submi ...

  9. 【POJ】1451 T9

    DFS+字典树. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Tr ...

随机推荐

  1. SQL性能优化十条经验(转)

    1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'-- 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...

  2. 走进Node.js 之 HTTP实现分析

    作者:正龙(沪江Web前端开发工程师) 本文为原创文章,转载请注明作者及出处 上文"走进Node.js启动过程"中我们算是成功入门了.既然Node.js的强项是处理网络请求,那我们 ...

  3. sessionStorage、localStorage 存储及如何存储数组与对象

    1.存储,获取,清楚 sessionStorage.setItem("key",val) sessionStorage.getItem("key") sessi ...

  4. 点聚合功能---基于ARCGIS RUNTIME SDK FOR ANDROID

    一直不更新博客的原因,如果一定要找一个,那就是忙,或者说懒癌犯了. 基于ArcGIS RunTime SDK for Android的点聚合功能,本来是我之前做过的一个系统里面的一个小部分,今天抽出一 ...

  5. 无向图广度优先遍历及其matlab实现

    广度优先遍历(breadth-first traverse,bfts),称作广度优先搜索(breath first search)是连通图的一种遍历策略.之所以称作广度优先遍历是因为他的思想是从一个顶 ...

  6. Python自学笔记-paramiko模块(Mr serven)

    文章出处:http://www.cnblogs.com/wupeiqi/articles/5095821.html SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: #!/u ...

  7. ZOJ 2002 Copying Books 二分 贪心

    传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如     ...

  8. Oracle 定时查询数据插入新表中(job+存储过程)

    create table EGMAS_COUNT_DATA(TIMES       date not null, COUNT NUMBER(30) not null, SYSTEM_NAME VARC ...

  9. webpack 的使用1

    进入指定文件夹  npm init 安装 npm install webapck --save-dev 根目录下新建hello.js 将文件打包到指定文件  Asset :打包成的文件名称 Chunk ...

  10. 在SQL中用正则表达式替换html标签(2)

    由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都替换掉.当然我可以通过写一个程序去修改,那毕竟有点麻烦.直接在查询分析器中执行,但是MS SQL Server并没有 ...