fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. bool is_anagram(string a, string b)
  8. {
  9. sort(a.begin(), a.end());
  10. sort(b.begin(), b.end());
  11. return a == b;
  12. }
  13.  
  14. int main()
  15. {
  16. vector< vector<string> > list;
  17. const int array_size = 6;
  18. string array[array_size] = { "seaside", "scent", "postman", "disease", "cents", "tampons" };
  19.  
  20. // Store strings.
  21.  
  22. bool match;
  23.  
  24. for (int i = 0; i < array_size; ++i)
  25. {
  26. match = false;
  27.  
  28. for (int j = 0; !match && j < list.size(); ++j)
  29. {
  30. if (is_anagram(array[i], list[j][0]))
  31. {
  32. list[j].push_back(array[i]);
  33. match = true;
  34. }
  35. }
  36.  
  37. if (!match)
  38. {
  39. vector<string> t;
  40. list.push_back(t);
  41. list[list.size() - 1].push_back(array[i]);
  42. }
  43. }
  44.  
  45. // Sort columns.
  46.  
  47. for (int i = 0; i < list.size(); i++)
  48. {
  49. sort(list[i].begin(), list[i].end());
  50. }
  51.  
  52. // Sort rows.
  53.  
  54. sort(list.begin(), list.end(), [](vector<string> a, vector<string> b)
  55. {
  56. return a[0] < b[0];
  57. });
  58.  
  59. // Display vector.
  60.  
  61. for (int i = 0; i < list.size(); i++)
  62. {
  63. for (int j = 0; j < list[i].size(); j++)
  64. {
  65. cout << list[i][j] << ' ';
  66. }
  67.  
  68. cout << endl;
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
cents scent 
disease seaside 
postman tampons