1002.Phone Numbers
来源:互联网 发布:天涯明月刀染色数据 编辑:程序博客网 时间:2024/06/05 15:56
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
73251890875ityourrealityrealour42949672965ityourrealityrealour-1
reality ourNo solution.
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
vector<string> outstr;
int Checkstr(){
string phonenum ;
cin>>phonenum;
if(phonenum.size() == 2 and phonenum[0] == '-' and phonenum[1] == '1'){
return -1;
}
int count;
cin>>count;
vector <string> instr;
vector <int> numstr;
int sequence = 0;
for(int i = 0 ; i < count ; i++){
string tstr;
cin>>tstr;
instr.insert(instr.end(), tstr);
}
for(int i = 0 ; i < instr.size() ; i++){
int tse = 0;
for(int j = 0 ; j < instr[i].size() ; j++){
int instrsq = -1;
switch (instr[i][j]) {
case 'i':;
case 'j':
instrsq = 1;
break;
case 'a':;
case 'b':;
case 'c':
instrsq = 2;
break;
case 'd':;
case 'e':;
case 'f':
instrsq = 3;
break;
case 'g':;
case 'h':
instrsq = 4;
break;
case 'k':;
case 'l':
instrsq = 5;
break;
case 'm':;
case 'n':
instrsq = 6;
break;
case 'p':;
case 'r':;
case 's':
instrsq = 7;
break;
case 't':;
case 'u':;
case 'v':
instrsq = 8;
break;
case 'w':;
case 'x':;
case 'y':
instrsq = 9;
break;
case 'o':;
case 'q':;
case 'z':
instrsq = 0;
break;
default:
break;
}
if(instrsq >=0 and (phonenum[sequence+j]-'0') == instrsq and tse != 1){
tse = 0;
cout<<"instr1 is "<<instr[i][j]<<endl;
}else{
cout<<"instr2 is "<<instr[i][j]<<endl;
tse = 1;
break;
}
}
if(tse == 0){
sequence = (int)instr[i].size() + sequence;
outstr.insert(outstr.end(), instr[i]);
}
}
return 0;
}
int main(int argc, const char * argv[]) {
vector<string> ostr;
while (1) {
if(Checkstr() == -1){
for(int i = 0 ; i < ostr.size() ; i++){
cout<<ostr[i]<<endl;
}
break;
}
else{
string stemp;
if(outstr.size()<1){
ostr.insert(ostr.end(), "No solution.");
}
else{
for(int i = 0 ; i < outstr.size() ; i++){
stemp = stemp + outstr[i] + ' ';
}
ostr.insert(ostr.end(), stemp);
outstr.clear();
}
}
}
return 0;
}
阅读全文
0 0
- 1002. Phone Numbers
- URAL 1002. Phone Numbers
- 1002.Phone Numbers
- Timus : 1002. Phone Numbers 题解
- Phone Numbers
- timus 1002. Phone Numbers(KMP&动态规划)
- ural 1002. Phone Numbers tire+spfa
- POJ 1732 Phone numbers
- ural 1002 Phone numbers
- ural 1002 phone numbers
- 【动态规划】Phone Numbers
- poj 1732 Phone numbers
- B. Phone Numbers
- B. Phone numbers
- [leetcode]Valid Phone Numbers
- Leetcode: Valid Phone Numbers
- Valid Phone Numbers
- Valid Phone Numbers
- 在gazebo中启动移动机器人模型,并实现横向移动
- HTML5
- 设计模式_7:模板方法模式
- 图形编程之 OpenGL ES 2.0 —— 从我们的世界到他们的世界 (3)
- DOS窗口下生成WebService客户端
- 1002.Phone Numbers
- spring-boot-actuator
- Loadrunner脚本录制&执行
- 二进制包和源代码安装的差距
- linux上安装memcached
- 南阳理工_31五个数求最值
- 【PAT】1009. 说反话
- [LeetCode]23. Merge k Sorted Lists
- Android 获取证书签名以及key hash散列值