1002. Phone numbers
Time Limit: 2.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
In the present world you frequently meet a lot of call numbers andthey are going to be longer and longer. You need to remember such akind of numbers. One method to do it in an easy way is to assignletters 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 uniquenumber, so you can remember words instead of call numbers. It isevident that it has its own charm if it is possible to find somesimple relationship between the word and the person itself. So you canlearn that the call number 941837296 of a chess playing friend ofyours can be read as WHITEPAWN, and the call number 2855304 of yourfavourite teacher is read BULLDOG.
Write a program to find the shortest sequence of words (i.e. onehaving the smallest possible number of words) which corresponds to agiven number and a given list of words. The correspondence isdescribed by the picture above.
Input
Input contains a series of tests. The first line of each test contains the call number, thetranscription of which you have to find. The number consists of atmost 100 digits. The second line contains the total number of thewords in the dictionary (maximum is 50 000). Each of the remaininglines contains one word, which consists of maximally 50 small lettersof the English alphabet. The total size of the input doesn'texceed 300 KB. The last line contains call number −1.
Output
Each line of output contains the shortest sequenceof words which has been found by your program. The words are separatedby single spaces. If there is no solution to the input data, the linecontains text “
No solution.
”. If there are more solutions having theminimum 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
每个单词对应到唯一数字串,可以将每个单词转换成数字串,存入哈希表中,最后再将电话号码分段在哈系表中查找。
1.在哈希表中,关键字是数字串,由于要求输出原单词,所以还要保存该数字串对应的原单词,幸好题目不要求按照字典顺序。
2.将电话号码分段然后到哈系表中查找用dp,t[a][b]表示由a开始由b结束([a, b),半开半闭区间,下同)的串最少能用多少个单词组成,那么t[a][b] = min{t[a][k], [k][b]}(k > a && k <= b)。设n为电话号码长度。可以从t[0][1]...t[n-1][n](段长度为1)开始,求到t[0][n](段长度为n) 假如哈希表查找时间复杂度为O(1),那么dp时间复杂度为O(n^3)。