T9
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.
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 |
Sample Output
Scenario #1: |
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的更多相关文章
- POJ 1451 T9
T9 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3083 Accepted: 1101 Description Ba ...
- http://blog.csdn.net/bluejoe2000/article/details/39521405#t9
http://blog.csdn.net/bluejoe2000/article/details/39521405#t9
- hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)
Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...
- hdu 1298 T9(特里+DFS)
pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...
- POJ 1451 - T9 - [字典树]
题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- 玩爆你的手机联系人--T9搜索(一)
自己研究了好几天联系人的T9搜索算法, 先分享出来给大家看看. 欢迎不吝赐教.假设有大神有更好的T9搜索算法, 那更好啊,大家一起研究研究,谢谢. 第一部分是比較简单的获取手机联系人. 获取 ...
- 套题T8&T9
A - 8球胜负(eight) Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submi ...
- 【POJ】1451 T9
DFS+字典树. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Tr ...
随机推荐
- OpenStack Ocata 超详细搭建文档
前言 搭建前必须看我本文档搭建的是分布式O版openstack(controller+ N compute + 1 cinder)的文档.openstack版本为Ocata.搭建的时候,请严格按照文档 ...
- css常用属性2
1 浮动和清除浮动 在上篇的第十一节--定位中说道: CSS 有三种基本的定位机制:普通流.浮动和绝对定位. 普通流和绝对定位已经说完,接下来就是浮动了. 什么是浮动? CSS 的 Float(浮动 ...
- 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome
Minimum palindrome Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- C# 使用FileUpload控件上传图片,将文件转换成二进制进行存储与读取
状况描述: 需要上传文件,但是不想要保存到实体路径下,便可以用该功能来实现. 效果图: 点击[Upload]按钮,上传文件到数据库: 点击[Preview],预览文件: 具体实现: 前台: <t ...
- Java 继承、抽象、接口
一.继承 1. 概述 继承是面向对象的重要特征之一,当多个类中存在相同的属性和行为时,将这些内容抽取到单独一个类中,那多个类中无需再定义这些属性和行为,只需继承那个单独的类即可. 单独的类称为父类或超 ...
- python contextlib 上下文管理器
1.with操作符 在python中读写文件,可能需要这样的代码 try-finally读写文件 file_text = None try: file_text = open('./text', 'r ...
- django celery的分布式异步之路(二) 高并发
当你跑通了前面一个demo,博客地址:http://www.cnblogs.com/kangoroo/p/7299920.html,那么你的分布式异步之旅已经起步了. 性能和稳定性是web服务的核心评 ...
- JS中的作用域以及全局变量的问题
一. JS中的作用域 1.全局变量:函数外声明的变量,称为全部变量 局部变量:函数内部使用var声明的变量,称为局部变量在JS中,只有函数作用域,没有块级作用域!!!也就是说,if/for等有{}的结 ...
- angular添加,查找与全部删除
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 详解python命名空间和作用域
1.典型案例 先从几个典型的案例来看下名称空间及作用域对python代码运行的影响,请看下面几个代码实例及其执行结果,是否符合你的预期. 代码1:块作用域 if True: i = 1 print i ...