Link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1298
Question:
Problem descriptiona 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 cocould 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 contains 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 cocould 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
Keys 4, 3, 5, 5, and 6, one after the other, the phone offers you "I", "ID", then switches to "El", "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
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 shoshould 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.
Inputthe 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
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 ".
Outputthe 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, blocks t 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
Question:In the past, mobile phone input methods were very troublesome, because there were only nine keys and 26 letters, so a key also contains these letters. In this case, the number of times a letter may be typed. For example, key 5 has "jkl". If you want to enter K, you have to press it twice. This input method is very troublesome. So a company invented the T9 technical input method. This input method has many built-in English words. Based on the frequency of English appearance and whether there are other information, each letter can have the desired pre-selection words as long as it is pressed once. For example, if the input method only has one built-in word "hell", you only need to press 4355. Note that if there is a word "hell", all prefixes of the word "H", "he", and "El" also exist.
Analysis and Summary:This question seems to have a big relationship with the actual application. Create a dictionary tree as required. when inserting a word, add the frequency P along the way. Then, use DFS to find the word output with the highest frequency.
When I did this, I did not expect it to be 1A. I laughed.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <vector> using namespace STD; const int kind = 26; const int maxn = 15000; int cnt_node; vector <int> G [10]; char input [105]; char ans [105]; int num, Max; bool flag; struct node {int prefix; node * Next [kind]; void Init () {prefix = 0; memset (next, 0, sizeof (next) ;}} heap [maxn]; inline node * newnode () {heap [cnt_node]. init (); Return & heap [cnt_node ++];} void insert (node * root, char * STR, int N) {for (char * P = STR; * P; + + p) {int CH = * P-'A'; If (root-> next [CH] = NULL) Root-> next [CH] = newnode (); root = root-> next [CH]; root-> prefix + = n ;}} void DFS (node * root, char * STR, int POS) {// STR Save the output result if (root = NULL) return; If (Pos> = num) {If (root-> prefix> MAX) {strcpy (ANS, str); max = root-> prefix;} flag = true; return;} int u = input [POS]-'0'; For (INT I = 0; I <G [u]. size (); ++ I) {STR [POS] = G [u] [I] + 'a '; DFS (root-> next [G [u] [I], STR, POS + 1); STR [POS] = 0 ;}} int main () {int t, n, P, CAS = 1; char STR [105]; // The keyboard settings seem complicated... for (INT I = 0; I <10; ++ I) g [I]. clear (); G [2]. push_back (0), g [2]. push_back (1), g [2]. push_back (2); G [3]. push_back (3), g [3]. push_back (4), g [3]. push_back (5); G [4]. push_back (6), g [4]. push_back (7), g [4]. push_back (8); G [5]. push_back (9), g [5]. push_back (10), g [5]. push_back (11); G [6]. push_back (12), g [6]. push_back (13), g [6]. push_back (14); G [7]. push_back (15), g [7]. push_back (16), g [7]. push_back (17), g [7]. push_back (18); G [8]. push_back (19), g [8]. push_back (20), g [8]. push_back (21); G [9]. push_back (22), g [9]. push_back (23), g [9]. push_back (24), g [9]. push_back (25); scanf ("% d", & T); While (t --) {scanf ("% d", & N ); printf ("Scenario # % d: \ n", CAS ++); // trie init cnt_node = 0; node * root = newnode (); For (INT I = 0; I <n; ++ I) {scanf ("% S % d", STR, & P); insert (root, STR, P );} scanf ("% d", & N); For (INT I = 0; I <n; ++ I) {scanf ("% s", input ); for (Int J = 0; j <strlen (input)-1; ++ J) {memset (STR, 0, sizeof (STR); max =-1; num = J + 1; flag = false; DFS (root, STR, 0); If (FLAG) puts (ANS); else puts ("manually ");} puts ("");} puts ("");} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)