HDU-1298-T9 [dictionary tree + Deep Search]

Source: Internet
Author: User
Tags repetition

T9


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1273 Accepted Submission (s): 506

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

Code:

The basic idea of this solution is:

Each node does not store a single letter or not, but all the letters before the letter, that is, a string,

At the end of the search, you only need to output this string.

The disadvantage is that each search starts from the first number input, to the first output string, and then outputs a string from the first number to the second number, and so on, this is a waste of time.

Therefore, it is better to store a letter at the node and store each result on the stack. If the node does not meet the conditions, the stack will be released. In this way, the reserved data will always meet the conditions.

# Include "stdio. h "# include" stdlib. h "# include" string. h "# include" stack "# include" iostream "using namespace std; typedef struct node {int val; node * next [26]; char word [105]; // save all previous letters} node; node memory [100010]; // allocate space for arrays to save time int cnt = 0, Max; node * tree; // The header node char map [10] [5], in [105], res [105]; void set () // define map [] [] {strcpy (map [0], ""); strcpy (map [1], ""); strcpy (map [2], "abc"); strcpy (map [3], "def"); strcpy (map [4], "ghi"); strcpy (map [5], "jkl"); strcpy (map [6], "mno"); strcpy (map [7], "pqrs"); strcpy (map [8], "tuv"); strcpy (map [9], "wxyz");} node * create () {node * p; int I; p = & memory [cnt ++]; p-> val = 0; for (I = 0; I <26; I ++) p-> next [I] = NULL; return p;} void insert (char c [], int val) {node * p; char str [105]; p = tree; int I, j; for (I = 0; I <strlen (c); I ++) {j = c [I]-'A '; if (p-> next [j] = NULL) {p-> next [j] = create ();} p = p-> next [j]; p-> val + = Val; str [I] = c [I]; str [I + 1] = 0; strcpy (p-> word, str ); // save all the previous letters, that is, a string for easy output} void dfs (node * p, int cur, int end) // cur: current position, number of the number, end: Destination position {if (cur = end) {if (p-> val> Max) // locate a maximum priority {strcpy (res, p-> word); // output this string Max = p-> val;} return;} int k, n, I, j; k = in [cur + 1]-'0'; // starting from the next number,-'0' converts a numeric number to a number. // printf ("k: % d \ n ", k); n = strlen (map [k]); // indicates several letters, three or four ~ // Printf ("n: % d \ n", n); // puts (map [k]); for (I = 0; I <n; I ++) {j = map [k] [I]-'A'; // start with the first letter. if there is any, continue the deep search. if not, check the next letter if (p-> next [j] = NULL) continue; else dfs (p-> next [j], cur + 1, end) ;}} int main () {int t, cas, n, val, I; char str [105]; scanf ("% d ", & t); for (cas = 1; cas <= t; cas ++) {memset (memory, 0, sizeof (memory); cnt = 0; tree = create (); scanf ("% d", & n); for (I = 0; I <n; I ++) // enter the word to create the dictionary tree {scanf ("% s % d", str, & val); insert (str, val) ;} printf ("S Cenario # % d: \ n ", cas); scanf (" % d ", & n); set (); while (n --) {scanf (" % s ", in); for (I = 0; in [I]! = '1'; I ++) // If you input a few numbers, there will be a-1 loop. Each number is the destination position {Max =-1; dfs (tree,-1, i); //-1 is the number of-1 if (Max! =-1) printf ("% s \ n", res); else printf ("MANUALLY \ n");} if (n! = 0) printf ("\ n");} printf ("\ n");} return 0 ;}

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.