hdu1298 T9 (cell phone Input method, each press a number, find the most frequently occurring string, the dictionary tree +dfs)

Source: Internet
Author: User
Tags cas repetition strlen
Problem Description a while ago it is quite cumbersome to create a message for the short message Service (SMS) on A Mobil E phone. This is because nine keys and the alphabet had more than nine letters, so most characters could only be ENT Ered by pressing one key several times. For example, if your 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 was very tedious and keeps many people from using the short Message Service.

This led manufacturers of the mobile phones to try and the find an easier-on-a-to-enter text on a mobile phone. The solution they developed is called T9 text input. The "9" in the name means so can enter almost arbitrary words with just nine keys and without pressing them more tha N once per character. The idea of the solution is so you simply start typing the keys without repetition, and the software uses a built-in DIC Tionary to 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 being the input for the word "GDJJM", but since this is no sensible 中文版 word, it can safely be Ignored. By ruling-all other "improbable" solutions and only taking proper 中文版 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 have to be 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 had found u P to the point. Let us assume this 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".

Write a 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 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 has the same probability, your program was to select the first one in alphabetic order. The user should also is able to type the beginning of words. For example, if the word "Hello" was 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 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 is given in the next W lines. (They is not guaranteed in ascending alphabetic order, although it ' s a dictionary.) Every line starts with the word which are 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 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 of in most of the decimal digits 2-9, followed by a single 1 meaning "next Word ".

Output the output for each scenario begins with a line containing "scenario #i:", where I am the number of the scenario St Arting 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 rule S 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 the every S Cenario.

Sample Input
2 5 Hell 3 Hello 4 idea 8 next 8 Super 3 2 435561 43321 7 Another 5 contest 6 follow 3 give integer 6 new + program 4 5 77647261 6391 4681 26684371 77771
Sample Output
Scenario #1: I id hel hell hello i id ide idea Scenario #2: P PR Pro Prog progr progra program n NE new g in int C Co con Cont anoth anothe Another p PR manually manually test instructions: First given n set of strings s and number A, indicating that the string s input a, followed by q input, each input indicates the key sequence (using the phone input hair type), Ask every keystroke may appear string, according to the output of high frequency, if not the output manually note: Each number represents a letter, if you want to find two length of the answer, then in the two length of all possible to find the most frequent occurrence.
#include <stdio.h> #include <string.h>//#include <malloc.h> typedef struct NN {int prio;
struct NN *next[26];
}node;
    typedef struct NNN {int maxprio;
Char str[105];

}ansseme;
    Node *builde () {int i;
    Node *p=new node;//node *p= (node*) malloc (sizeof (node));
    p->prio=0;
    for (i=0;i<26;i++) p->next[i]=null;
return p;
} node *root;
    void Insert (char str[],int prio) {node *p=root;
    int i;
        for (i=0;str[i]!= '; i++) {if (p->next[str[i]-' a ']==null) p->next[str[i]-' a ']=builde ();
        P=p->next[str[i]-' a '];
    p->prio+=prio;
}} int L;
Char num[105];
Ansseme prin[105];
Char ch[10][5]={"abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"};
    void Dfs (int i,node *p,char pre[]) {int Len=strlen (ch[num[i]-' 2 ')), ti=0,cc[4],pri[4],j; for (j=0;j<len;j++) if (p->next[ch[num[i]-' 2 '][j]-' a ']!=null) {pri[ti]=p->next[ch[num[i]-' 2 '][j]-
        ' A ']->prio; Cc[ti++]=ch[num[i]-' 2 '][j]-' a ';
        } for (j=0;j<ti;j++) {pre[i]=cc[j]+ ' a '; pre[i+1]= ';
        if (Prin[i].maxprio<pri[j]) {prin[i].maxprio=pri[j]; strcpy (PRIN[I].STR,PRE);
    } if (l>i+1) DFS (I+1,P-&GT;NEXT[CC[J]],PRE);
    }} int main () {int t,cas=0,n,prio,i;
    Char str[105];
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        printf ("Scenario #%d:\n", ++cas);
        Root=builde ();
            while (n--) {scanf ("%s%d", Str,&prio);
        Insert (Str,prio);
        } scanf ("%d", &n);
            while (n--) {scanf ("%s", num);
            L=strlen (num)-1;
            for (i=0;i<=l;i++) prin[i].maxprio=0;

             str[0]= ' + ';
             if (l) DFS (0,ROOT,STR);
             for (i=0;i<l;i++) if (Prin[i].maxprio) printf ("%s\n", prin[i].str);
     else printf ("manually\n");      printf ("\ n");
    } printf ("\ n");
 }
}


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.