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

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目4

    2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...

  9. 《Cracking the Coding Interview》——第11章:排序和搜索——题目3

    2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...

随机推荐

  1. Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.错误

    错误如图示: 1.在php的目录下建立个文件夹tmp,这个有权限的问题,如果是ntfs的分区,就一定要添加evryone的控制权限,否则是没用的.2.在php.ini找到session.save_pa ...

  2. 使用selenium grid遇到的坑,解决不了冲突,只有避免

    背景:使用到grid做分发,已经有两周,运行较稳定,分发也健壮,不知道是不是要因为运行量小,服务器也没出问题,稳定到两周后,发现分发到A服务器(10.40.2.113)和B服务器(10.40.2.11 ...

  3. UESTC 31 饭卡 card

    dp,答案容易想到是 凑出价格总和≤m-5 + 没被使用的最大价格. dp[i = 前i种价格][j = 价格总和] = 最大没使用的价格下标idx_m. dp[i-1][j]存在的话,则只要更新id ...

  4. POJ - 3470 Walls

    小鸟往四个方向飞都枚举一下,数据范围没给,离散以后按在其中一个轴线排序,在线段树上更新墙的id,然后就是点查询在在哪个墙上了. 这题有个trick,因为数据范围没给我老以为是inf设置小了,WA了很多 ...

  5. POJ-3041 Asteroids---二分图&最小覆盖点

    题目链接: https://vjudge.net/problem/POJ-3041 题目大意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍, 最少要几次. 解 ...

  6. php图像处理插件imagick安装(仅适用于86位,php5.4非安全环境-16px)

    phpImageMagick-6.7.7-5-Q16-windows-dll(加测试代码,经测试,仅适用于86位,php5.4安全环境-16px) 下载地址:http://pan.baidu.com/ ...

  7. Spring boot 异常处理配置

    1.    新建Maven项目 exception 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  8. 安装gcc,g++

    安装gcc.g++ sudo apt-get install make gcc g++ 查看g++版本 g++ --version

  9. _default_ VirtualHost overlap on port 80, the first has precedence

    去掉#NameVirtualHost *:80,然后重启httpd

  10. 第13章 GPIO-位带操作—零死角玩转STM32-F429系列

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...