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 repetition


Problem Descriptiona while ago it is quite cumbersome to create a message for the short message Service (SMS) on a mobile 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.

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

Outputthe output for each scenario begins with a line containing "scenario #i:", where I am the number of the scenario STA Rting 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 13
integer 6
new 14
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
The meaning of the title: first give n sets of string s and the number a, indicating that the string s has been input a times, then q times input, each input indicates the key sequence (using the phone input to send the kind), ask each time button Possible strings, according to the frequency of use, if not, output MANUALLY
Note: Each number represents a letter. If you want to find two lengths of the answer, find the highest frequency among all the possibilities of the two lengths.
#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]!='\0';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]='\0';
        if(prin[i].maxprio<pri[j])
        {
            prin[i].maxprio=pri[j]; strcpy(prin[i].str,pre);
        }
        if(l>i+1) dfs(i+1,p->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]='\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");
    }
}


Related Article

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.