1002. Phone Numbers
Time limit: 2.0 second
Memory limit: 64 MB
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
input | output |
---|---|
7325189087 5 it your reality real our 4294967296 5 it your reality real our -1 | reality our No solution. |
Problem Source: Central European Olympiad in Informatics 1999
题意:
给定一个数字串和若干个单词,根据手机按键用单词数将数字串拼出来。
求最少单词数对应的单词组合。
分析:
设数字串num的长度为l,前i个数字对应的最少单词数为f(i), 如果从i开始有一个单词word(j)能够匹配num(i+1, i+word(j).length),
则可以更新:f(i+word(j).length) = min(f(i+word(j).length), f(i)+1)
题目要求是要求出对应的单词组合,可以在递推过程中保存从f(i+word(j).length) 到 f(i)的指针以及对应的单词编号来记录,代码中使用了lw数组完成这项工作。
代码:
1 // 2 // main.cpp 3 // phoneNumber2 4 // 5 // Created by apple on 16/1/30. 6 // Copyright © 2016年 apple. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 #include <stack> 12 #include <map> 13 //数字串长度 14 #define maxl 1000 15 //单词个数 16 #define maxn 100 17 //最多单词数 18 #define maxw 100 19 //最大值 20 #define inf 100000 21 22 using namespace std; 23 struct node { 24 node(int a, int b, bool v) { 25 fa = a; 26 wn = b; 27 vrfd = v; 28 } 29 node () { 30 vrfd = false; 31 } 32 int fa, wn; 33 bool vrfd; 34 }; 35 //每个长度对应的最少单词数,初始化为inf 36 int f[maxl+1]; 37 //每个长度对应最少单词数的单词编号队 38 node lw[maxl+1]; 39 //单词们 40 vector<string> words; 41 stack<int> ans; 42 string num; 43 map<char, char> phone; 44 45 46 int match(int pl, int wn) { 47 for (int i = 0; i < words[wn].length(); i++) { 48 if (pl == num.length() || phone[words[wn][i]] != num[pl]) 49 return -1; 50 pl++; 51 } 52 return pl; 53 } 54 int main(int argc, const char * argv[]) { 55 phone['o'] = phone['q'] = phone['z'] = '0'; 56 phone['w'] = phone['x'] = phone['y'] = '9'; 57 phone['t'] = phone['u'] = phone['v'] = '8'; 58 phone['p'] = phone['r'] = phone['s'] = '7'; 59 phone['m'] = phone['n'] = '6'; 60 phone['k'] = phone['l'] = '5'; 61 phone['g'] = phone['h'] = '4'; 62 phone['d'] = phone['e'] = phone['f'] = '3'; 63 phone['a'] = phone['b'] = phone['c'] = '2'; 64 phone['i'] = phone['j'] = '1'; 65 int wn, i, j, tmp1; 66 string tmp; 67 lw[0] = node(-1, -1, true); 68 while (cin >> num && num != "-1") { 69 words.clear(); 70 cin >> wn; 71 while (wn--) { 72 cin >> tmp; 73 words.push_back(tmp); 74 } 75 f[0] = 0; 76 for (i = 1; i <= maxl; i++) { 77 f[i] = inf; 78 } 79 for (i = 1; i <= maxl; i++) { 80 lw[i] = node(-1,-1,false); 81 } 82 for (i = 0; i < num.length(); i++) { 83 for (j = 0; j < words.size(); j++) { 84 if (lw[i].vrfd && (tmp1 = match(i, j)) != -1 && f[tmp1] > f[i]+1) { 85 f[tmp1] = f[i] + 1; 86 lw[tmp1] = node(i, j, true); 87 } 88 } 89 } 90 if (!lw[num.length()].vrfd) { 91 cout << "No solution." << endl; 92 } else { 93 int itr = num.length(); 94 while (itr != 0) { 95 ans.push(lw[itr].wn); 96 itr = lw[itr].fa; 97 } 98 while (ans.size() != 1) { 99 cout << words[ans.top()] << " "; 100 ans.pop(); 101 } 102 cout << words[ans.top()] << endl; 103 ans.pop(); 104 } 105 } 106 }