Algorithm title: Hdu 1298 T9 (mobile input method related, dictionary tree +dfs)

Source: Internet
Author: User
Tags integer repetition

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1298

Topic:

Problem Description

A while ago it is quite cumbersome to create a, the short message Service (SMS) on a mobile phone. This is because your only have nine keys and the alphabet has more than nine-letters, so most characters could Ered by pressing one key several The times. For example, if you are wanted to type "Hello" 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 are very tedious and keeps many people from using the "short" message Service.

This led manufacturers of the mobile phones to try and find a easier way to enter text on a mobile phone. The solution they developed is called T9 text input. The "9" in the name means this can enter almost arbitrary words with just nine keys and without pressing N once per character. The idea of the "solution is" you simply start typing the keys without repetition, and the software uses a built-in DIC Tionary to look for the "most probable" word matching the input. For example, to enter "Hello" simply press keys 4, 3, 5, 5, and 6 once. Of course, this could also are the input for the word "GDJJM", but since this is no sensible 中文版 word, it can safely be Ignored. By ruling out all the other "improbable" solutions and only taking proper 中文版, this can words up Writing of short messages considerably. Of course, if the word is isn't in the dictionary (like a name) then it has to being typed in manually using key repetition Aga In.

Figure 8:the Number-keys of a mobile phone.

More precisely, with every character typed, the phone would show the most probable combination of characters it has found U p to which 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 for 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. The probability of a character combination is defined to being the sum of the probabilities of all words in the dictionary th At begin with this character combination. For example, if the dictionary contains three words "hell", "Hello", and "hellfire", the probability of the character comb Ination "Hell" is the sum of the probabilities of these words. If some combinations have the same probability, your program are to select the "the one in alphabetic order." The user should also is 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 isn't listed in the dictionary.

Input

The 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 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 was a sequence of lowercase letters from the alphabet without, whitespace by A spaces and an integer p, 1<=p<=100, representing the probability of this word. No word would contain more than letters.

Following the dictionary, there is a-line containing a single integer m. Next follow M lines, each consisting of a sequence to most decimal digits 2-9, followed by a single 1 meaning "next Word.

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.