1002. Phone Numbers
2013-04-06 00:00
288 查看
1002. Phone Numbers
Time Limit: 2.0 secondMemory 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 |
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. |
dp[i] 表示第i个数字时最少用几个单词
状态转移方程为
dp[i + word[j].length()-1] = max(dp[i-1] + 1,dp[i + word[j].length()-1]);
注意记录时要记录路径
#include <iostream> #include <vector> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #define MAX 100 + 10 #define INF 100000000 using namespace std; string phoneNum; vector <string> dict(10); int dp[MAX]={0},path[MAX] = {0}; void initDict(){ dict[0]="oqz"; dict[1]="ij"; dict[2]="abc"; dict[3]="def"; dict[4]="gh"; dict[5]="kl"; dict[6]="mn"; dict[7]="prs"; dict[8]="tuv"; dict[9]="wxy"; } bool match(string number, string word){ for(int i = 0 ; i < number.length(); i ++){ int len = dict[number[i]-'0'].length(),j; for( j = 0; j < len; j ++ ){ if(dict[number[i]-'0'][j] == word[i]) break; } if(j >= len) return false; } return true; } int main(){ initDict(); while(cin >> phoneNum && phoneNum != "-1"){ int n; cin>>n; vector<string> word(n); for(int i = 0; i < n; i ++ ) cin >>word[i]; for(int i = 0; i < MAX; i ++ ) dp[i] = INF; memset(path,-1,sizeof(path)); dp[0] = 0; for(int i = 1; i <= phoneNum.length(); i ++ ){ for(int j = 0; j < n; j ++ ){ if( (word[j].length() + i-1) <= phoneNum.length() && match( phoneNum.substr(i-1,word[j].length()) , word[j] ) ){ if(dp[i + word[j].length()-1] > dp[i-1] + 1){ dp[i + word[j].length()-1] = dp[i-1] + 1; path[i + word[j].length()-1] = j; } } } } if(path[phoneNum.length()] == -1) cout<<"No solution."<<endl; else{ vector <string> ans; int k = phoneNum.length(); while(path[k] != -1){ ans.push_back(word[path[k]]); k = k - word[path[k]].length(); } int ansLen = ans.size(); cout<<ans[ansLen-1]; for(int i = ansLen-2; i >= 0 ; i-- ) cout<<" "<<ans[i]; cout<<endl; } } return 0; }
相关文章推荐
- timus 1002. Phone Numbers(KMP&动态规划)
- ural 1002. Phone Numbers tire+spfa
- ural 1002 Phone numbers
- ural 1002 phone numbers
- Timus 1002. Phone numbers
- Timus : 1002. Phone Numbers 题解
- 1002. Phone Numbers
- URAL 1002. Phone Numbers
- URAL 1002 Phone Numbers
- 1002.Phone Numbers
- URAL 1002 Phone Numbers
- Timus : 1002. Phone Numbers 题解
- Valid Phone Numbers
- Validate International Phone Numbers (验证国际电话号码)
- Valid Phone Numbers
- B. Phone numbers
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 193. Valid Phone Numbers
- codeforces Round #451 (Div. 2) Phone Numbers
- [leetcode]Valid Phone Numbers