1002. Phone Numbers

Time limit: 2.0 second
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数组完成这项工作。
 
代码:
 //
// main.cpp
// phoneNumber2
//
// Created by apple on 16/1/30.
// Copyright © 2016年 apple. All rights reserved.
// #include <iostream>
#include <vector>
#include <stack>
#include <map>
//数字串长度
#define maxl 1000
//单词个数
#define maxn 100
//最多单词数
#define maxw 100
//最大值
#define inf 100000 using namespace std;
struct node {
node(int a, int b, bool v) {
fa = a;
wn = b;
vrfd = v;
}
node () {
vrfd = false;
}
int fa, wn;
bool vrfd;
};
//每个长度对应的最少单词数,初始化为inf
int f[maxl+];
//每个长度对应最少单词数的单词编号队
node lw[maxl+];
//单词们
vector<string> words;
stack<int> ans;
string num;
map<char, char> phone; int match(int pl, int wn) {
for (int i = ; i < words[wn].length(); i++) {
if (pl == num.length() || phone[words[wn][i]] != num[pl])
return -;
pl++;
}
return pl;
}
int main(int argc, const char * argv[]) {
phone['o'] = phone['q'] = phone['z'] = '';
phone['w'] = phone['x'] = phone['y'] = '';
phone['t'] = phone['u'] = phone['v'] = '';
phone['p'] = phone['r'] = phone['s'] = '';
phone['m'] = phone['n'] = '';
phone['k'] = phone['l'] = '';
phone['g'] = phone['h'] = '';
phone['d'] = phone['e'] = phone['f'] = '';
phone['a'] = phone['b'] = phone['c'] = '';
phone['i'] = phone['j'] = '';
int wn, i, j, tmp1;
string tmp;
lw[] = node(-, -, true);
while (cin >> num && num != "-1") {
words.clear();
cin >> wn;
while (wn--) {
cin >> tmp;
words.push_back(tmp);
}
f[] = ;
for (i = ; i <= maxl; i++) {
f[i] = inf;
}
for (i = ; i <= maxl; i++) {
lw[i] = node(-,-,false);
}
for (i = ; i < num.length(); i++) {
for (j = ; j < words.size(); j++) {
if (lw[i].vrfd && (tmp1 = match(i, j)) != - && f[tmp1] > f[i]+) {
f[tmp1] = f[i] + ;
lw[tmp1] = node(i, j, true);
}
}
}
if (!lw[num.length()].vrfd) {
cout << "No solution." << endl;
} else {
int itr = num.length();
while (itr != ) {
ans.push(lw[itr].wn);
itr = lw[itr].fa;
}
while (ans.size() != ) {
cout << words[ans.top()] << " ";
ans.pop();
}
cout << words[ans.top()] << endl;
ans.pop();
}
}
}
 

1002 Phone Numbers 解题报告的更多相关文章

  1. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  3. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. USACO Section2.2 Runaround Numbers 解题报告 【icedream61】

    runround解题报告---------------------------------------------------------------------------------------- ...

  6. [POJ 1002] 487-3279 C++解题报告

        487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 228365   Accepted: 39826 D ...

  7. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  8. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  9. 洛谷 CF55D Beautiful numbers 解题报告

    CF55D Beautiful numbers 题意 \(t(\le 10)\)次询问区间\([l,r](1\le l\le r\le 9\times 10^{18})\)中能被每一位上数整除的数的个 ...

随机推荐

  1. DOM相关属性,方法,兼容性问题处理小析

    DOM:Document Object Model文档对象模型,用于让程序(js)取操作页面中的元素.DOM节点类型有12种. (一)属性 一.子节点操作1.所有子节点(1)元素.childNodes ...

  2. PHP的开发环境

    快速搭建工作环境,测试环境,开始PHP的编程之旅. liunx CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL) 准备篇: 1.配置防火墙,开启80端口.3306端口v ...

  3. memcache 笔记

    在windows下安装memecache,进入到安装目录,执行帮助命令可以查看到相关帮助 启动memcache,相关命令如上,比如-p就是端口号,memcache默认端口号是11211 语法:增加ad ...

  4. chrome浏览器 模拟访问移动端

    谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页: 谷歌Android ...

  5. java读取项目中文件路径及乱码解决

    this.getClass.getResource(path).getPath(); 如果出现中文乱码,可以使用java.net.URLDecoder.decode方法进行处理 如:URLDecode ...

  6. 保护眼睛(ubuntu 和 chrome)

    chrome 安插件https://chrome.google.com/webstore/detail/%E4%BF%9D%E6%8A%A4%E7%9C%BC%E7%9D%9B/fgadnbmmoln ...

  7. ps 文字处理篇

    ps文字处理篇 1.对文字镂空处理并且移除到新图像上: 首先创建图层-文字编辑-横排编辑 其次 魔棒工具选择通过颜色来选择选区 右击图层-栅格化图层 删除键将选择的颜色删除留下选区- 复制粘贴到另一个 ...

  8. alternatives命令用法

    alternatives是Linux下的一个功能强大的命令.只能在根权限下执行.如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用替代指定一个全局的设置.alternatives常用于同一个 ...

  9. git常使用命令整理

    1.git撤销本地所有修改(tracked和untracked) . git clean -df . git reset --hard 第一个命令只删除所有untracked的文件,如果文件已经被tr ...

  10. 采用DBCP连接池技术管理连接

    DBCP的使用步骤步骤一:导包,使用第三方的道具,必须导入相应的jar包. 一般需要导入两个jar包: -commons-dbcp-1.x.jar包 -commons-pool-1.x.x.jar包 ...