Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07
原题:
Mapping
'' = 'A','B','C'
'' = 'D','E','F'
...
'' = input:
output :ouput = [AAD, BBD, CCD, AAE, AAF, BBE, BBF, CCE, CCF]
题目:电话的数字按键和字母有个映射关系,给定一串数字,请给出所有可能的字符映射方式。
解法:此人也不给个描述,搞的下面一堆人来猜题意。这个题目的意思是说,对于给定的数字串,有多少种不同的映射方式。像“112” -> “ABD”这样是不允许的,因为“1”只能表示一种字母,不能同时表示“A”和“B”。
代码:
// http://www.careercup.com/question?id=5765850736885760
#include <iostream>
#include <string>
#include <vector>
using namespace std; void DFS(int idx, vector<pair<int, char> > &dict, vector<string> &result, const string &s)
{
int i; if (idx == (int)dict.size()) {
static char m[]; for (i = ; i < (int)dict.size(); ++i) {
m[dict[i].first] = dict[i].second;
} static string res; res.clear();
for (i = ; i < (int)s.length(); ++i) {
res.push_back(m[s[i] - '']);
} result.push_back(res);
return;
} switch(dict[idx].first) {
case :
case :
case :
case :
case :
case :
case :
case :
for (i = ; i < ; ++i) {
dict[idx].second = (dict[idx].first * + i) + 'A';
DFS(idx + , dict, result, s);
}
break;
case :
for (i = ; i < ; ++i) {
dict[idx].second = (dict[idx].first * + i) + 'A';
DFS(idx + , dict, result, s);
}
break;
}
} int main()
{
int i;
string s;
vector<pair<int, char> > dict;
vector<string> result;
int a[]; while (cin >> s) {
for (i = ; i < ; ++i) {
a[i] = ;
}
for (i = ; i < (int)s.length(); ++i) {
a[s[i] - ''] = ;
} for (i = ; i < ; ++i) {
if (a[i]) {
dict.push_back(make_pair(i, '\0'));
}
} DFS(, dict, result, s);
cout << "{" << endl;
for (i = ; i < (int)result.size(); ++i) {
cout << " " << result[i] << endl;
}
cout << "}" << endl; dict.clear();
result.clear();
} return ;
}
Careercup - Facebook面试题 - 5765850736885760的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- 使用ambari搭建Hadoop平台
1.操作系统 CentoOS Server with GUI(有GUI,有浏览器*ambari基于浏览器*推荐latest stable version)2.分区 默认 + /hadoop3.网络设置 ...
- SQL里IN的用法以及优化
1.in后条件不多,可以考虑主表建索引,或用union all 代替 2. in 和 exists的区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查 ...
- cocos2dx-lua之断点调试支持
cocos2dx 3.2版对cocos code ide支持已经相当棒了,不过话说,编辑器用起来感觉没有sublime顺手 支持cocos code ide已经支持创建lua项目了,可是默认创建的项目 ...
- Hadoop集群错误
1.Hadoop集群所有的DataNode都启动不了解决办法 删除从节点.../usr/hadoop/tmp/dfs/ 下内容,再重新格式化namenode
- berkerly db 中简单的读写操作(有一些C的 还有一些C++的)
最近在倒腾BDB,才发现自己确实在C++这一块能力很弱,看了一天的api文档,总算是把BDB的一些api之间的关系理清了,希望初学者要理清数据库基本知识中的环境,句柄,游标的基本概念,这样有助于你更好 ...
- 5.servlet cookie自动登录的实例
1.要建的文档,.java用servlet创建 2.建一张登陆表格 index.jsp <%@ page language="java" import="java. ...
- CentOS学习笔记--基本命令--目录的相关操作
Linux基本命令--目录的相关操作 常见的处理目录的命令吧: cd:变换目录 pwd:显示目前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cd (变换目录) cd是Chang ...
- webstorm 添加文件模板
Ctrl+Shift+A 搜索设置 File Teamplate 添加 File Teamplate
- 【Qt】Qt国际化【转】
简介 Qt国际化属于Qt高级中的一部分,本想着放到后面来说,上节刚好介绍了Qt Linguist,趁热打铁就一起了解下. 对于绝大多数的应用程序,在刚启动时,需要加载默认的语言(或最后一次设置的语言) ...
- Sublime Text 前端开发常用扩展插件推荐
Sublime Text 前端开发常用扩展插件推荐 Sublime Text Sublime Text 是程序员们公认的编码神奇,拥有漂亮的用户界面和强大的功能 更重要的是,Sublime Text ...