2014-05-02 09:59

题目链接

原题:

Group Anagrams
input = ["star, astr, car, rac, st"]
output = [["star, astr"],["car","rac"],["st"]);

题目:给定一堆字符串,设法把anagram都排在一起。

解法:自定义一个comparator,互为anagram的两个字符串在排好序以后是相同的。根据这个规则可以写出个效率不怎么高,但是代码很短的算法。

代码:

 // http://www.careercup.com/question?id=5733320654585856
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std; string ss1, ss2;
bool comparator(const string &s1, const string &s2)
{
ss1 = s1;
ss2 = s2; sort(ss1.begin(), ss1.end());
sort(ss2.begin(), ss2.end()); return ss1 < ss2;
} int main()
{
int i;
int n;
vector<string> v; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
sort(v.begin(), v.end(), comparator);
cout << "{" << endl;
for (i = ; i < (int)v.size(); ++i) {
cout << " " << v[i] << endl;
}
cout << "}" << endl;
v.clear();
} return ;
}

Careercup - Facebook面试题 - 5733320654585856的更多相关文章

  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面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  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. 零碎记录Hadoop平台各组件使用

    >20161011 :数据导入研究    0.sqoop报warning,需要安装accumulo:    1.下载Microsoft sql server jdbc, 使用ie下载,将42版j ...

  2. 默认样式重置 (css reset)

    body,p,h1,h2,h3,h4,h5,h6,dl,dd,t{margin:0; font-size:12px;/* font-family:XX; */} ol,ul{list-style:no ...

  3. .NET程序编译和运行

    一次面试的时候遇到的一道题目,简要说明.NET的编译过程,在网上看了很多资料,简单总结如下: 1.一般的编译过程 通常高级语言的程序编译过程是:首先写好的程序是源代码,然后编译器编译为本地机器语言,最 ...

  4. 跟我一起学习ASP.NET 4.5 MVC4.0(一)(转)

    由于上面一个项目使用的是ASP.NET4.0 MVC3.0,在招人的时候发现很多人有听说过MVC,但是却是没用过,对MVC也只是一知半解,最近想给团队成员做一个系统的解说,让大家都可以学习一下ASP. ...

  5. php学习笔记6--php中的文件包含 include,require,include_once,require_once

    php中的文件包含 include,require,include_once,require_once 文件包含:是指将一个文件的内容包含进另外一个文件,有利于代码的复用等.php中文件包含指令有4个 ...

  6. xenserver xensource.log不断增长

    转载:http://blog.sina.com.cn/s/blog_4ca83f830100xded.html     相信很多人被Xenserver日志填满磁盘空间,导致机器最终挂掉的问题所困扰,我 ...

  7. 快速调试chromium

    上一篇我们简单的将了在Ubuntu上编译chromium,android content_shell_apk的编译,一切顺利的就能生成apk.但是我们仅仅只是照搬了人家google开源的东西,作为一个 ...

  8. Android Bitmap那些事之如何优化内存

    前言:”安得广厦千万间,大庇天下寒士俱欢颜“——杜甫.在帝都住的朋友们都可能会遇到租房子困难的问题(土豪请无视),找房子真是力气活,还耗费时间,占用我宝贵的写博客时间,没办法,谁让咱没钱还想住的好点, ...

  9. oracle中编写java代码

    使用sql语句创建 create or replace and compile java source named test_java_source as package test_java_sour ...

  10. 10款经典的web前端特效的预览及源码

    1.CSS3响应式导航菜单 今天我给大家介绍一下如何使用纯CSS来实现的一个响应式导航菜单,我们使用的是HTML5+CSS3技术,当浏览器窗口变小或者使用手机浏览器访问的时候,原本横条菜单会收缩成一个 ...