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. 结合setTimeout和clearTimeout,实现“返回顶部”的功能

    结合setTimeout和clearTimeout,当页面停止滚动时,“返回顶部”按钮淡隐淡出.点击“返回顶部”页面以动画形式返回顶部.完美兼容ie6-11,firefox,chrome等. html ...

  2. CSS3 照片墙

    HTML <body> <h2>照片墙制作</h2> <div class="container"> <img class=& ...

  3. 用Drawing画图如何不会消失

    方法一:将事件放在form_Load中,在窗体中画图   1: protected void MainForm_Load(object sender,EventArgs e) 2: { 3: Init ...

  4. asp.net实现通用水晶报表

    此片博文是在你有一定水晶报表基础的前提下参阅的:如果对于水晶报表的基础知识比较薄弱建议先去了解下水晶报表: 因为项目需要,研究了下水晶报表.说实在,这个组件很强大,但是用起来也很麻烦.刚开始使用遇到了 ...

  5. Js中把JSON字符串转换为JSON对象(eval()、new Function())

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  6. jquery对象与js对象的相互转换

    jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...

  7. .NET XML文件增删改查

    查询 采用的是DataSet 的 ReadXML方法. DataSet ds = new System.Data.DataSet(); ds.ReadXml("bdc.xml"); ...

  8. 20141009---Visual Studio 2012 预定义数据类型

    预定义数据类型 一.值类型 整型:(整数) 有符号整型和无符号整形,区别是有符号的有负数无符号的都是正数, 2x+1 常用int 有符号:              带有正负数,范围为按所写依次增大 ...

  9. codeforces Round 286# problem A. Mr. Kitayuta's Gift

    Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked ...

  10. OpenGL第8,9讲小结

    这两节,透明度和物体的3D运动,主要集中在第9讲,因为第9讲也用到了通过Alpha值来调整透明度的地方. 因为要模拟星星,所以要创建的四边形需要很多,例子中创建了50个正方形.因为每个星星的属性都差不 ...