poj 1451 难度:中等 字典树+搜索
来源:互联网 发布:美即黑面膜怎么样 知乎 编辑:程序博客网 时间:2024/05/15 06:42
点击打开链接
T9
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4046 Accepted: 1438
Description
Background
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".
Problem
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.
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".
Problem
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 iven in the next w lines in ascending alphabetic order. 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�, followed by a single 1 meaning "next word".
Each scenario begins with a line containing the number w of distinct words in the dictionary (0<=w<=1000). These words are iven in the next w lines in ascending alphabetic order. 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�, 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.
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
25hell 3hello 4idea 8next 8super 32435561433217another 5contest 6follow 3give 13integer 6new 14program 4577647261639146812668437177771
Sample Output
Scenario #1:iidhelhellhelloiidideideaScenario #2:pprproprogprogrprograprogramnnenewginintccoconcontanothanotheanotherpprMANUALLYMANUALLY
题意:相信 大家用手机打字都用过九宫格吧,这题就是模拟九宫格打字。
思路:字典树+bfs,构造字典树后用广搜搜出频率最大的单词并输出,要注意九宫格的7,8,9这三个特殊的键,还有注意换行符的输入,这个题很容易wa(说多了都是泪),稍微细节没注意就wa。还要注意输入0的时候输出MANUALLY,或者搜索没搜到也要输出MANUALLY。
#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;struct node{int cnt;//记录频数 int step;//标记当前是第几个节点 char str[1001];//记录根节点到当前节点的部分字符串 struct node *next[26];};int Case=0;char s[1001];queue<node*>q;node *newset(){node *p=(node*)malloc(sizeof(node));p->cnt=p->step=0;for(int i=0;i<26;i++)p->next[i]=NULL;return p;}void insert(node *root,int key){int i,j,t,k,len=strlen(s);node *p=root;for(i=0;i<len;i++){t=s[i]-'a';if(p->next[t]==NULL)p->next[t]=newset();p->next[t]->step=p->step+1;p=p->next[t];p->cnt+=key;for(j=0;j<=i;j++)p->str[j]=s[j];p->str[j]='\0';}}void bfs(int num){int i,t,j,flag,max=0;char temp[101];int mark=0;if(q.empty()){printf("MANUALLY\n");//队列已经空了,之前就没搜到 return;}node *p=q.front();flag=p->step;while(!q.empty()){p=q.front();if(p->step>flag)break;q.pop();i=num*3-6;t=num*3-3;if(num==7)t++;else if(num>7)i++,t++;if(num==9)t++;for(;i<t;i++)if(p->next[i]!=NULL){mark=1;if(p->next[i]->cnt>max){max=p->next[i]->cnt;strcpy(temp,p->next[i]->str);}q.push(p->next[i]);}}if(!mark)printf("MANUALLY\n");//没搜到 elseprintf("%s\n",temp);}int main(){int T;scanf("%d",&T);while(T--){int n,m,k,i;node *root=newset();scanf("%d",&n);for(i=0;i<n;i++){scanf("%s%d",s,&k);insert(root,k);}scanf("%d",&m);printf("Scenario #%d:\n",++Case);while(m--){scanf("%s",s);while(!q.empty())q.pop();q.push(root);int len=strlen(s);for(int j=0;j<len;j++)if(s[j]=='0')printf("MANUALLY\n");else if(s[j]=='1')break;elsebfs(s[j]-'0');printf("\n");}printf("\n");}}
阅读全文
0 0
- poj 1451 难度:中等 字典树+搜索
- 实现中等难度通讯录(字典,属性,…
- 实现中等难度通讯录(字典,属性,…
- [dfs] Delicious cakes 中等难度的搜索
- 中等难度
- 中等难度的通讯录.字典 动态分组法
- poj 3694 中等难度的双…
- poj 3321 Apple Tree(树状数组+dfs)中等难度
- 中等难度通讯录
- hdu 1540 难度:中等
- POJ 1451 字典树
- OC实现中等难度通讯录
- 338. Counting Bits 难度:中等
- poj 2155 Matrix(二维树状数组,好题)中等难度题目,更新区域,查询单点
- POJ 1204 Word Puzzles // 字典树,枚举, 搜索
- [字典树]poj 1451 T9
- poj 1451 T9 字典树
- poj 1062 昂贵的聘礼 Dijkstra算法,中等难度,,,内有测试数据,一道让我累觉不爱的题目
- dyld: Library not loaded: @rpath/libswiftCore.dylib问题解决
- Kali linux渗透测试(七)
- TCP和UDP的区别和应用
- nio基本使用
- 【知了堂学习笔记】Model1、Model2和MVC架构模式
- poj 1451 难度:中等 字典树+搜索
- 线性表顺序结构增删改查
- STL库中的list的使用和模拟实现
- JS加载与运行
- JVM类加载过程,JDK和JER区别
- log4j学习笔记
- Product of Array Except Self
- [Java--面试]--阿里巴巴面试2016
- 二分查找,你真的懂吗