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 ...
随机推荐
- oracle sql 树操作
语法:select-start with-connect by-prior 主要有两点 1)prior放在子节点端,则表示扫描树是以start with指定的节点作为根节点从上往下扫描.可能对应一个或 ...
- Angular4 后台管理系统搭建(9) - 用自定义angular指令,实现在服务端验证
最近这段时间发现,北京这用angular4 或 angular2的公司很少.几乎是没有.很担心自己是不是把精力放到了不应该的地方.白耽误了时间.但是随着我对新版angular框架理解的加深.个人感觉a ...
- ASP.NET Core中如何调整HTTP请求大小的几种方式
一.前言 一般的情况下,我们都无需调用HTTP请求的大小,只有在上传一些大文件,或者使用HTTP协议写入较大的值时(如调用WebService)才可能会调用HTTP最大请求值. 在ASP.NET Co ...
- 『诡异的』VL10B创建外向交货单出错解决全过程
一直觉得SAP STO的业务模式配置起来还是挺简单的,无非就是关联一下采购单与交货单的关系,以及相应工厂的装运数据,其他像主数据的设置也没有什么特别的.相比ICS模式,它少了IDOC的配置,所以还是很 ...
- LCM Cardinality 暴力
LCM Cardinality Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- hdu 1542 线段树 求矩形并
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- SpringMVC的一点理解
1.MVC(Model-View-Controller) 用慕课网上的一个图来看看MVC Front Controller(前端控制器):把客户的请求分发给不同的控制器去生成业务数据,将生成的业务数据 ...
- Windows中的硬链接和软链接(hard link 和 Symbolic link)
先来了解一下Linux中的硬链接和软链接: Linux中的硬链接和软链接 Windows中的硬链接和软链接: 硬链接 从Windows NT4开始,NTFS文件系统引入了HardLink这个概念,它让 ...
- Intellij idea史上最简单的教程之Linux下安装与破解Intellij idea2017
一.前言 这一节我们介绍在Linux下如何安装与破解Intellij idea2017.现在有很多公司开发环境都是Linux,所以掌握在Linux环境下使用Idea办公也是咱们必须得掌握的技能. 记住 ...
- HDU3336 Count the string
居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...
