fork(2) download
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. string itoa(int n)
  12. {
  13. stringstream ss;
  14. ss <<n;
  15. return ss.str();
  16. }
  17.  
  18. string gen_hash(string s)
  19. {
  20. int count[26];
  21. string hash="";
  22. fill_n(count,26,0);
  23. for (int i = 0; i < s.size(); ++i)
  24. {
  25. ++count[s[i]-'a'];
  26. }
  27. for (int i = 0; i < 26; ++i)
  28. {
  29. if(count[i]!=0)
  30. {
  31. hash+=('a'+i);
  32. hash+=itoa(count[i]);
  33. }
  34. }
  35. return hash;
  36. }
  37.  
  38. int main()
  39. {
  40. int N;
  41. string S;
  42. map<string,set<string> > anagram_group;
  43. set<set<string> > sorted;
  44. cin >> N;
  45. for (int i = 0; i < N; ++i)
  46. {
  47. cin >> S;
  48. anagram_group[gen_hash(S)].insert(S);
  49. }
  50. for (map<string,set<string> >::iterator i = anagram_group.begin(); i != anagram_group.end(); ++i)
  51. {
  52. sorted.insert(i->second);
  53. }
  54. for (set<set<string> >::iterator i = sorted.begin(); i != sorted.end(); ++i)
  55. {
  56. for (set<string>::iterator j = i->begin(); j!= i->end() ; ++j)
  57. {
  58. cout<<*j<<" ";
  59. }
  60. cout<<"\n";
  61. }
  62. return 0;
  63. }
Success #stdin #stdout 0s 3488KB
stdin
7
aabbcc
acbabc
abccba
bcba
ab
aa
abbc
stdout
aa 
aabbcc abccba acbabc 
ab 
abbc bcba