《Cracking the Coding Interview》——第11章:排序和搜索——题目2
2014-03-21 20:49
题目:设计一种排序算法,使得anagram排在一起。
解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数。
代码:
// 11.2 Sort an array of strings such that anagrams stay next to each other.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std; string ta, tb;
int counting[]; void countingSort(string &s)
{
int i, j; for (i = ; i < ; ++i) {
counting[i] = ;
}
for (i = ; i < (int)s.length(); ++i) {
++counting[s[i]];
}
s.clear(); for (i = ; i < ; ++i) {
for (j = ; j < counting[i]; ++j) {
s.push_back(i);
}
}
} bool anagramComparator(const string &a, const string &b)
{
ta = a;
tb = b;
sort(ta.begin(), ta.end());
sort(tb.begin(), tb.end()); return ta < tb;
} int main()
{
vector<string> v;
int i;
int n; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
sort(v.begin(), v.end(), anagramComparator);
for (i = ; i < n; ++i) {
if (i > ) {
cout << ' ';
}
cout << v[i];
}
cout << endl;
} return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目2的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...
随机推荐
- anaconda和jupyter notebook使用方法
查看安装的conda版本 conda --version 如果没有安装anaconda,可以从以下链接下载anaconda或者miniconda,两者安装一个就可以了 miniconda大约50M h ...
- 【js基础修炼之路】— 深入浅出理解闭包
之前对于闭包的理解只是很肤浅的,只是浮于表面,这次深究了一下闭包,下面是我对闭包的理解. 什么是闭包? 引用高程里的话 => 闭包就是有权访问另一个作用域中变量的函数,闭包是由函数以及创建该函数 ...
- 正则表达式转换python2的print为python3风格
直接查找 print ([^\n\(]*)替换为 print($1)
- jwt 在.net core 2.0的使用
jwt个人觉得更适合作共享session的传递格式,本身保密性不好,容易泄露重要信息,他的格式为头.一些用户的自定义声明.前两者的加密(公私对称密钥形式) 需要引用nuget: System.Iden ...
- POJ-3126 Prime Path---BFS+素数打表
题目链接: https://vjudge.net/problem/POJ-3126 题目大意: 给两个四位数a,b 每次改变a中的一位而且改动之后的必须是素数,问最少改动几次可以到b?(永远达不到b就 ...
- nodejs 的一些PHP函数库
http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...
- cudaMallocPitch()
- Win7多用户同时登陆
软件提供下载: http://pan.baidu.com/s/1o6FQv70
- iclr2015
http://www.iclr.cc/doku.php?id=iclr2015:main#accepted_papers iclr2015的accept papers,有些看过,有些没看明白,看来还是 ...
- Hibernate进阶学习3
Hibernate进阶学习3 测试hibernate的多表关联操作(一对多,多对一,多对多) 表之间的关系主要在类与元数据配置文件中体现 package com.hibernate.domain; i ...