//原文:
//
// 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;
}
Cracking The Coding Interview
最新推荐文章于 2024-05-10 09:30:14 发布