(Dictionary tree application) poj 1451 T9

Source: Internet
Author: User
Tags repetition
T9
Time limit:1000 ms   Memory limit:10000 K
Total submissions:1809   Accepted:692

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 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 the 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 ".

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 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.

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 ".

Output

The output for each scenario begins with a line ining "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
/* Topic: Create a mobile phone dictionary and record the probability of using words. Then, press the number key on the keyboard. each time you press a number key, the most likely words are displayed on your phone. question solving: Construct a mobile phone dictionary in Lexicographic Order and accumulate the probability that each word prefix string appears. create a ing table (1-3) for numbers and letters, calculate the probability values of all possible prefix strings, and output the string with the highest probability; otherwise, output manually. code tip: the queue stores all prefix strings produced by each number, and uses STR + 'C' to overlay and generate a new string source codeproblem: 1451 User: wawadimu memory: 588 K time: 32 Ms language: C ++ result: accepted source code */# include <iostream> # include <string> # include <queue> using namespace STD; // 26-letter dictionary tree struct n Ode {int num; struct node * Next [26]; node () {num = 0; For (INT I = 0; I <26; I ++) next [I] = NULL ;}}; node * root; // root node // use an existing dictionary to construct the dictionary tree void insert (const char * STR, int V) {If (root = NULL) root = new node (); node * P = root; while (* Str) {int Index = (* Str)-'A '; * STR ++; If (p-> next [Index] = NULL) // The letter prefix does not exist, branch {P-> next [Index] = new node ();} p = p-> next [Index]; P-> num + = V; // cumulative probability value of this prefix string} int search (const char * Str) {node * P = Root; while (P & (* Str) {int Index = (* Str)-'A'; * STR ++; P = p-> next [Index];} If (! P) return 0; // return p-> num For This prefix string not found; // returns the probability of This prefix string .} const char key [27] = {"22233344455566677778889999"}; // map the asii value of str-'A' to Char s [102], t [102]; // s dictionary string, t search string int main () {// freopen ("1451.txt"," r ", stdin); // freopen (" output.txt "," W ", stdout); int case, N, I, J, K, P, Len; char C; scanf ("% d", & case); for (I = 1; I <= case; I ++) {printf ("Scenario # % d:/N", I); root = new node (); scanf ("% d ", & N); While (n --) // insert all lexicon {scanf ("% s % D ", S, & P); insert (S, P) ;}scanf (" % d ", & N); While (n --) // n search strings {scanf ("% s", T); queue <string> q; string LLS, STR; STR = ""; q. push (STR); For (k = 0; t [k]! = '1'; k ++) // prefix string {STR = ""; C = T [k]; int num, max = 0; // num returns the probability of a possible prefix string. Max returns the maximum probability of all possible strings. Len = Q. size (); While (Len --) // All strings produced by the previous string {string sub = Q. front (); q. pop (); For (j = 0; j <26; j ++) {If (C = Key [J]) // each number may correspond to three letters {lls = sub + char ('A' + J); // generate a new string num = search (LLS. c_str (); If (max <num) // find the maximum probability of each possible prefix string {max = num; STR = lls;} If (num! = 0) Q. push (LLS); // The dictionary does not contain this string or the dictionary} else if (C <key [J]) break; // all three letters are calculated, exit loop} If (max) printf ("% s/n", str. c_str (); else // without the string prefix, print manually directly to read the next string {While (T [k]! = '1') {k ++; printf ("manually/N") ;}break ;}} printf ("/N ");} printf ("/N");} return 1 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.