1002. Phone Numbers

来源:互联网 发布:手机视频特技软件 编辑:程序博客网 时间:2024/05/16 10:47

1002. Phone Numbers

Time Limit: 2.0 second
Memory Limit: 16 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 def4 gh    5 kl    6 mn7 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
73251890875ityourrealityrealour42949672965ityourrealityrealour-1
reality ourNo solution.


dp,其实可以不用这么长的,(把switch语句换一种表示方式就可以了

还有,memset的第二个参数不是十进制的,所以除了0或NULL的初始化,不要用memset!


#include <iostream>using namespace std;char s[150], a[50001][51], num[50001][51];int f[100], state[100][100], n, length;int match(int n, int k){for (int i=0; num[k][i]; n++,i++)if (num[k][i] - s[n] != 0)return -1;return n;}int main(){int i, j;cin>>s;while (s[0] - '-'){memset(a, NULL, 50001*51*sizeof(char));memset(num, NULL, 50001*51*sizeof(char));//memset(f, 10000, 200*sizeof(int));for (i=0; i<100; i++)f[i] = 10000;//memset(state, -1, 100*100*sizeof(int));for (i=0; i<100; i++)for (j=0; j<100; j++)state[i][j] = -1;f[0] = 0;length = strlen(s);cin>>n;for (i=0; i<n; i++)cin>>a[i];for (i=0; i<n; i++)for (j=0; a[i][j]!=NULL; j++)switch(a[i][j]){case 'i':;case 'j':num[i][j] = '1';break;case 'a':;case 'b':;case 'c':num[i][j] = '2';break;case 'd':;case 'e':;case 'f':num[i][j] = '3';break;case 'g':;case 'h':num[i][j] = '4';break;case 'k':;case 'l':num[i][j] = '5';break;case 'm':;case 'n':num[i][j] = '6';break;case 'p':;case 'r':;case 's':num[i][j] = '7';break;case 't':;case 'u':;case 'v':num[i][j] = '8';break;case 'w':;case 'x':;case 'y':num[i][j] = '9';break;case 'o':;case 'q':;case 'z':num[i][j] = '0';break;default:;}for (i=0; i<length; i++){if (i!=0 && f[i]==10000)continue;for (j=0; j<n; j++){int temp = match(i, j);if (temp != -1 && temp <= length && f[temp] > f[i] + 1){f[temp] = f[i] + 1;int temp1;for (temp1=0; temp1<100; temp1++)state[temp][temp1] = -1;for (temp1=0; state[i][temp1]!=-1; temp1++)state[temp][temp1] = state[i][temp1];state[temp][temp1] = j;}}}for (i=0; state[length][i]!=-1; i++){cout<<a[state[length][i]]<<" ";}if (i == 0)cout<<"No solution.";cout<<endl;cin>>s;}}


原创粉丝点击