zoukankan      html  css  js  c++  java
  • 1002. Phone Numbers

    1002. Phone Numbers

    Time Limit: 2.0 second
    Memory Limit: 64 MB
    In the present world you frequently meet a lot of call numbers and they are going to be longer and longer. You need to remember such a kind of numbers. One method to do it in an easy way is to assign letters to digits as shown in the following picture:
    1 ij    2 abc   3 def
    4 gh    5 kl    6 mn
    7 prs   8 tuv   9 wxy
            0 oqz
    
    This way every word or a group of words can be assigned a unique number, so you can remember words instead of call numbers. It is evident that it has its own charm if it is possible to find some simple relationship between the word and the person itself. So you can learn that the call number 941837296 of a chess playing friend of yours can be read as WHITEPAWN, and the call number 2855304 of your favourite teacher is read BULLDOG.
    Write a program to find the shortest sequence of words (i.e. one having the smallest possible number of words) which corresponds to a given number and a given list of words. The correspondence is described by the picture above.

    Input

    Input contains a series of tests. The first line of each test contains the call number, the transcription of which you have to find. The number consists of at most 100 digits. The second line contains the total number of the words in the dictionary (maximum is 50 000). Each of the remaining lines contains one word, which consists of maximally 50 small letters of the English alphabet. The total size of the input doesn't exceed 300 KB. The last line contains call number −1.

    Output

    Each line of output contains the shortest sequence of words which has been found by your program. The words are separated by single spaces. If there is no solution to the input data, the line contains text “No solution.”. If there are more solutions having the minimum number of words, you can choose any single one of them.

    Sample

    inputoutput
    7325189087
    5
    it
    your
    reality
    real
    our
    4294967296
    5
    it
    your
    reality
    real
    our
    -1
    
    reality our
    No solution.
    

    此题利用动态规划解决,

      dp[i] 表示第i个数字时最少用几个单词

      状态转移方程为

        dp[i + word[j].length()-1] = max(dp[i-1] + 1,dp[i + word[j].length()-1]);

         注意记录时要记录路径

     1 #include <iostream>
     2 #include <vector>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <string>
     7 
     8 #define MAX  100 + 10
     9 #define INF 100000000
    10 using namespace std;
    11 
    12 string phoneNum;
    13 vector <string> dict(10);
    14 int dp[MAX]={0},path[MAX] = {0};
    15 
    16 void initDict(){
    17     dict[0]="oqz";  dict[1]="ij";   dict[2]="abc"; dict[3]="def";
    18     dict[4]="gh";   dict[5]="kl";  dict[6]="mn";   dict[7]="prs";
    19     dict[8]="tuv";  dict[9]="wxy";
    20 }
    21 
    22 bool match(string number, string word){
    23     for(int i = 0 ; i < number.length(); i ++){
    24         int len = dict[number[i]-'0'].length(),j;
    25         for( j = 0; j < len; j ++ ){
    26             if(dict[number[i]-'0'][j] == word[i]) break;
    27         }
    28         if(j >= len) return false;
    29     }
    30     return true;
    31 }
    32 
    33 int main(){
    34     initDict();
    35     while(cin >> phoneNum && phoneNum != "-1"){
    36         int n;
    37         cin>>n;
    38         vector<string> word(n);
    39         for(int i = 0; i < n; i ++ ) cin >>word[i];
    40         for(int i = 0; i < MAX; i ++ ) dp[i] = INF;
    41         memset(path,-1,sizeof(path));
    42         dp[0] = 0;
    43         for(int i = 1; i <= phoneNum.length(); i ++ ){
    44             for(int j = 0; j < n; j ++ ){
    45                 if( (word[j].length() + i-1) <= phoneNum.length() && match( phoneNum.substr(i-1,word[j].length()) , word[j] ) ){
    46                     if(dp[i + word[j].length()-1] > dp[i-1] + 1){
    47                         dp[i + word[j].length()-1] = dp[i-1] + 1;
    48                         path[i + word[j].length()-1] = j;
    49                     }
    50                 }
    51             }
    52 
    53         }
    54         if(path[phoneNum.length()] == -1) cout<<"No solution."<<endl;
    55         else{
    56             vector <string> ans;
    57             int k = phoneNum.length();
    58             while(path[k] != -1){
    59                 ans.push_back(word[path[k]]);
    60                 k = k - word[path[k]].length();
    61             }
    62             int ansLen = ans.size();
    63             cout<<ans[ansLen-1];
    64             for(int i = ansLen-2; i >= 0 ; i-- ) cout<<" "<<ans[i];
    65             cout<<endl;
    66         }
    67     }
    68 
    69     return 0;
    70 }
  • 相关阅读:
    HTTP基础及telnet简单命令
    【详解】并查集高级技巧:加权并查集、扩展域并查集
    二叉树中两节点的最近公共父节点(360的c++一面问题)
    (用POJ 1160puls来讲解)四边形不等式——区间dp的常见优化方法
    详细讲解Codeforces Round #625 (Div. 2)E
    详细讲解Codeforces Round #625 (Div. 2) D. Navigation System
    详细讲解Codeforces Round #624 (Div. 3) F. Moving Points
    详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
    POJ
    新手建站前需要做哪些准备?
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3001815.html
Copyright © 2011-2022 走看看