zoukankan      html  css  js  c++  java
  • HDU 1298 T9(字典树+dfs)

    T9

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


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

    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 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 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".
     
    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
    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
     
    Source
     
    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1251 1075 1671 2846 1305 
     
    题目大意:模拟手机键盘的九个按键输入法,首先给出整数w,然后输入w个字符串和每个字符串的出现频率,然后输入一个整数m,后面紧跟m行数字字符串,字符串以‘1’结束,问当依次按下数字字符串对应的按钮时,最有可能输入的字符串。如果存在多个,则输出字典序较小的。
    解题方法:先用字典序保存每个字符串和字符串中每个字符出现的次数,然后用DFS搜索。

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <string>
      4 #include <algorithm>
      5 #include <iostream>
      6 using namespace std;
      7 
      8 typedef struct node
      9 {
     10     int time;
     11     node *nxt[26];
     12 }treenode;
     13 
     14 char num[1005], ans[1005], strtemp[1005];
     15 int ntime;
     16 char op[10][5] = { { "" },{ "" },{ "abc" },{ "def" },{ "ghi" },{ "jkl" },{ "mno" },{ "pqrs" },{ "tuv" },{ "wxyz" } };
     17 
     18 treenode*builde()
     19 {
     20     treenode*p = (treenode*)malloc(sizeof(node));
     21     int i;
     22     p->time = 0;
     23     for (i = 0; i < 26; i++)
     24     {
     25         p->nxt[i] = 0;
     26     }
     27     return p;
     28 }
     29 
     30 void insert(treenode *root, char s[], int time)//建立字典树
     31 {
     32     int l = strlen(s);
     33     treenode *p = root;
     34     int i;
     35     for (i = 0; i < l; i++)
     36     {
     37         if (p->nxt[s[i] - 'a']== NULL)
     38         {
     39             p->nxt[s[i] - 'a'] = builde();
     40         }
     41         p->nxt[s[i] - 'a']->time += time;
     42         p = p->nxt[s[i] - 'a'];
     43     }
     44 }
     45 
     46 void find(treenode *root, int l, int index)
     47 {
     48     treenode*p = root;
     49 
     50     //如果长度达到了要查找的长度,则判断出现次数是否比当前已经查找过的字符串出现次数多
     51     if (l == index)//达不到长度就无法return,知道所有字母遍历完
     52     {
     53         if (p->time > ntime)
     54         {
     55             strcpy_s(ans, strtemp); // 更新字符串
     56             ntime = p->time;//更新次数
     57         }
     58         return;
     59     }
     60     for (int i = 0; i < strlen(op[num[index] - '0']); i++)//遍历该数字下的每一个字母
     61     {
     62         char temp= op[num[index] - '0'][i];
     63         if (p->nxt[temp - 'a'] == NULL) continue;
     64         strtemp[index] = temp;
     65         strtemp[index + 1] = '';
     66         find(p->nxt[temp - 'a'], l, index + 1);//要找的长度+1
     67     }
     68 }
     69 
     70 int main()
     71 {
     72     int k, n, ncount = 0;
     73     treenode *root = NULL;
     74     char str[1005];
     75     cin >> k;
     76     while (k--)
     77     {
     78         cin >> n;//有几个单词
     79         printf("Scenario #%d:
    ", ++ncount);
     80         root = builde();
     81         int i,j;
     82         for (i = 0; i < n; i++)//输入字符串
     83         {
     84             cin >> str >> ntime;
     85             insert(root, str, ntime);//插入单词
     86         }
     87         cin >> n;//有几个数字
     88         for (i = 0; i < n; i++)
     89         {
     90             cin >> num;
     91             for (j = 0; j < strlen(num) - 1; j++)//对每个数字遍历
     92             {
     93                 ntime = 0;//用于记录最大次数
     94                 find(root, j + 1, 0);//j+1是遍历到的长度
     95                 if (ntime == 0)
     96                 {
     97                     printf("MANUALLY
    ");
     98                 }
     99                 else
    100                 {
    101                     printf("%s
    ", ans);
    102                 }
    103             }
    104             cout << endl;
    105         }
    106         free(root);
    107         cout << endl;
    108     }
    109     return 0;
    110 }
    
    
    
     
  • 相关阅读:
    【go语言学习】标准库之sync
    【go语言学习】并发实现
    【go语言学习】并发概念
    【html学习】html基础
    【go语言学习】错误error和异常panic
    【go语言学习】type关键字
    【go语言学习】面向对象oop
    【go语言学习】接口interface
    【go语言学习】结构体struct
    【go语言学习】指针pointer
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8450239.html
Copyright © 2011-2022 走看看