1002 Phone Numbers 解题报告
1002. Phone Numbers
Memory limit: 64 MB
1 ij 2 abc 3 def |
Input
Output
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 |
reality our |
//
// 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 解题报告的更多相关文章
- 【九度OJ】题目1442:A sequence of numbers 解题报告
[九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- USACO Section2.2 Runaround Numbers 解题报告 【icedream61】
runround解题报告---------------------------------------------------------------------------------------- ...
- [POJ 1002] 487-3279 C++解题报告
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 228365 Accepted: 39826 D ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- 洛谷 CF55D Beautiful numbers 解题报告
CF55D Beautiful numbers 题意 \(t(\le 10)\)次询问区间\([l,r](1\le l\le r\le 9\times 10^{18})\)中能被每一位上数整除的数的个 ...
随机推荐
- 【Alpha】Daily Scrum Meeting第三次
本次随笔调换了展示顺序,把重要的内容放前面. 一.本次Daily Scrum Meeting主要内容 说明要完成alpha版本还需要哪些功能 对这些功能进行分析和实现方式的讨论 强调编码规范和变量命名 ...
- npm link 安装本地模块,将本地模块cli化
第三方学习地址 http://mp.weixin.qq.com/s?__biz=MzAxMTU0NTc4Nw==&mid=2661157390&idx=1&sn=6d96e54 ...
- SharePoint Configuration Wizard - Unable to upgrade SharePoint Products and Technologies because an upgrade is already in progress
故障描述 当要运行SharePonit Products and Technologies Configuration Wizard的时候,出现了如下图所示的错误提示. 错误信息为: Unable t ...
- Java实现Oracle数据库备份
今天将很早就实现的一个Oracle数据库备份功能粘贴出来,这个功能是在大学做阶段设计时写的: import java.io.File; import java.io.IOException; /** ...
- centos部署gitlab服务器
gitlab的安装和配置非常简单,关于git,这里摘抄一下百度百科: Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git是一个开源的分布式版本控制系统,可以有效. ...
- CPP - sort
#include "stdafx.h" #include <iostream> #include <string> using namespace std; ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- Java 枚举7常见种用法
DK1.5引入了新的类型--枚举.在 Java 中它虽然算个"小"功能,却给我的开发带来了"大"方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: ...
- vert.x学习(八),用JDBCClient配合c3p0操作数据库
今天学习了下vert.x的JDBCClient,我这里将今天的学习笔记记录下来.这次学习中使用了c3p0. 用使用JDBCClient和c3p0得现在pom.xml文件里面导入对应的依赖,下面贴出xm ...
- merge 本地 master 分支代码提示 “Already up-to-date”
在使用 git 的过程中由于误操作,导致从本地 master 分支 merge 代码到当前分支失败,虽然当前分支和 master 分支代码不同步,但是仍然提示 Already up-to-date. ...