I'm using a map that first sorts the words by the number of letters they appear, and then sort the output.
Note that if a word appears more than two times, then the output group size is also counted, but the output word is the time to output the word only once.
#include <iostream> #include <cstdio> #include <memory.h> #include <string> #include <map
> #include <algorithm> #include <functional> #include <vector> using namespace std;
Char buf[500];
Map<map<int,int>,vector<string> >s;
Vector<vector<string> >ans; BOOL CMP (const Vector<string>&v1,const VECTOR<STRING>&V2) {return v1.size () >v2.size () | |
(V1.size () ==v2.size () &&v1<v2);
} int main () {while (scanf ("%s", buf)!=eof) {map<int,int>cnt;
int Len=strlen (BUF);
for (int i=0;i<len;++i) {cnt[buf[i]]++;
} s[cnt].push_back (String (BUF));
} map<map<int,int>,vector<string> >::iterator It=s.begin ();
for (; It!=s.end (); ++it) {sort (It->second.begin (), It->second.end ());
Ans.push_back (It->second);
} sort (Ans.begin (), Ans.end (), CMP);
int acnt=5; for (int i=0;acnt&&i<ans.size (); ++i,--acnt) {printf ("Group of size%d:", Ans[i].size ());
Ans[i].erase (Unique (Ans[i].begin (), Ans[i].end ()), Ans[i].end ());
for (int j=0;j<ans[i].size (); ++j) {printf ("%s", Ans[i][j].c_str ());
} printf (". \ n");
} return 0; }