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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  4. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  5. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  6. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. HttpContext 讲解

    HttpContext类:封装有关个别HTTP请求的所有HTTP特定的信息,又叫上下文.看到这个解释,我觉得有些抽象,Http特定信息具体又是什么?看了下备注:为继承 IHttpModule 和 IH ...

  2. Part 9 Sorting data in AngularJS

    To sort the data in Angular 1. Use orderBy filter     {{ orderBy_expression | orderBy : expression : ...

  3. linux下usb驱动接口中端点介绍

    端点 USB 通讯的最基本形式是通过一个称为端点的东西.一个USB端点只能向一个方向传输数据(从主机到设备(称为输出端点)或者从设备到主机(称为输入端点)).端点可被看作一个单向的管道. 一个 USB ...

  4. 银联接口测试——详细(JAVA)

    准备材料 1.注册账号 https://open.unionpay.com/ajweb/register?locale=zh_CN 2.▼登录账号 -->帮助中心--> 下载,选择网关支付 ...

  5. pop()实现逐个删除数组最后一位并输出

    使用pop()循环输出数组的最后一个元素 var a = []; a.push(1);a.push(3.1415926);a.push("number");a.push(" ...

  6. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  7. VS默认环境设置

    VS2010的工具菜单-->导入导出设置-->重置所有设置

  8. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  9. ADO.NET笔记——基本概念

    ADO.NET中的主要对象: Connection:连接对象.用于建立从应用程序到数据库服务器指定数据库的连接通道 Command:命令对象.用于执行增删查改等数据库语句命令 DataReader:数 ...

  10. JavaIO和JavaNIO

    BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...