(字典樹應用) poj 1451 T9

來源:互聯網
上載者:User
T9
Time Limit: 1000MS   Memory Limit: 10000K
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 could 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 many 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 could 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 "hel", "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 should 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 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, 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 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
/*題目大意:構建一個手機詞典並記錄單詞使用的機率,然後使用者按手機鍵盤上面的數字鍵.要求使用者每按一個數字鍵,手機彈出可能性最大的單詞.題目求解:用字典序構造手機詞典,並累計每一個單詞首碼串出現的機率. 然後建立數字和字母的映射表(1-3),計算所有可能首碼串的機率值,:並輸出其中的機率最大的字串,否則輸出MANUALLY.代碼技巧:佇列儲存體每個數字產生的所有首碼串,用str+'c'來疊加產生新的字串Source CodeProblem: 1451  User: wawadimu Memory: 588K  Time: 32MS Language: C++  Result: Accepted Source Code */#include <iostream>#include<string>#include<queue>using namespace std;//26個字母的字典樹struct node{    int num;    struct node* next[26];    node()    {        num=0;        for(int i=0;i<26;i++)        next[i]=NULL;    }};node *root;//根節點//用已有詞庫構造字典樹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)//不存在這個字母的首碼,分支        {            p->next[index]=new node();        }        p=p->next[index];        p->num+=v;//累計這個首碼串出現的機率值    }}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;//返回這個首碼串的機率.}const char key[27]={"22233344455566677778889999"};//映射鍵盤對應的str-'a'的ASII值char s[102],t[102];//s詞庫串,t尋找串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--)//插入所有詞庫        {            scanf("%s%d",s,&p);            insert(s,p);        }        scanf("%d",&n);        while(n--)//n個尋找串        {            scanf("%s",t);            queue<string> q;            string lls,str;            str="";            q.push(str);            for(k=0;t[k]!='1';k++)//首碼串            {                str="";                c=t[k];                int num,max=0;//num返回可能首碼串的機率,max返回所有可能串的最大機率值                len=q.size();                while(len--)//上一個字串產生的所有串                {                    string sub=q.front();                    q.pop();                    for(j=0;j<26;j++)                    {                        if(c==key[j])//每一個數字可能對應三個字母                        {                            lls=sub+char('a'+j);//產生新字串                            num=search(lls.c_str());                            if(max<num)//找每一個可能的首碼串的最大機率                            {                                max=num;                                str=lls;                            }                            if(num!=0) q.push(lls);//詞庫中不包含這個字串或詞庫                        }                        else if(c<key[j]) break;//三個字母全部計算過,退出迴圈                    }                }                if(max)printf("%s/n",str.c_str());                else//沒有字串首碼,直接全部列印MANUALLY,去讀取下一個字串                {                    while(t[k]!='1')                    {                        k++;                        printf("MANUALLY/n");                    }                    break;                }            }            printf("/n");        }        printf("/n");    }    return 1;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.