Vulkan

Cracking The Coding Interview

//原文:
//
//	Write a method to sort an array of strings so that all the anagrams are next to each other.
//	译文:
//	写一个函数对字符串数组排序,使得所有的变位词都相邻。//参考@hawstein

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool cmp(string a, string b)
{
	sort(&a[0], &a[0] + a.size());
	sort(&b[0], &b[0] + b.size());
	return a<b;
}
int main()
{
	string a[6] = {"bsc","sad","kucf","csb","bas","fcuk"};

	sort(a, a+6, cmp);
	for (int i=0; i<6; i++)
	{
		cout<<a[i]<<"  ";
	}
	return 0;
}

posted on 2014-04-23 14:40  Vulkan  阅读(152)  评论(0编辑  收藏  举报

导航