Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. /*******************************************************************************
  2. * Name : anagramfinder.cpp
  3. * Author : Christopher Tan
  4. * Version : 1.0
  5. * Date : 10/25/19
  6. * Description : Anagram finder.
  7. * Pledge : I pledge my honor that I have abided by the Stevens Honor System.
  8. ******************************************************************************/
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <cctype>
  13. #include <map>
  14. #include <set>
  15. #include <vector>
  16. #include <bits/stdc++.h>
  17. #include <stdio.h>
  18. #include <errno.h>
  19.  
  20. using namespace std;
  21.  
  22. int main(int argc, char* argv[]) {
  23. ifstream fs;
  24. fs.open(argv[1]);
  25. if (argc != 2) {
  26. cerr << "Usage: ./anagramfinder <dictionary file>" << endl;
  27. return 1;
  28. }
  29. FILE * pFile;
  30. pFile = fopen (argv[1] , "r");
  31. if (pFile == NULL) {
  32. cerr << "Error: File '" << argv[1] << "' not found." << endl;
  33. return 1;
  34. }
  35. ifstream file(argv[1]);
  36.  
  37.  
  38.  
  39.  
  40. // sort the output so the words in each group are alphabetized as well as the overall result.
  41. // map similarly should store key in a sorted fashion.
  42. map<string, vector<string>> anagrams_map;
  43. string word;
  44.  
  45. // track the keyset corresponding to the largest group(s) of anagrams
  46. set<string> result_keys;
  47. long unsigned largest_group_size = 0;
  48.  
  49. while (file >> word) {
  50. // process just the words containing upper case letters A through Z and
  51. // lowercase letters a through z.
  52. bool is_valid_word = all_of(begin(word), end(word), [](char c) { return isalpha(c); });
  53. if (not is_valid_word) {
  54. continue;
  55. }
  56.  
  57. // when finding anagrams, take a case-insenstive approach
  58. // strategy here is to use the sorted, lowercase version of a word as the "key" in our map.
  59. string key = word;
  60. transform(key.begin(), key.end(), key.begin(), [](unsigned char c) { return tolower(c); } );
  61. sort(key.begin(), key.end());
  62. // grab the group of anagrams for this word so far.
  63. vector<string> &anagrams = anagrams_map[key];
  64. anagrams.emplace_back(word);
  65.  
  66. if (result_keys.empty()) {
  67. result_keys.emplace(key);
  68. largest_group_size = 1;
  69. } else {
  70. if (anagrams.size() < largest_group_size) {
  71. // not big enough.
  72. continue;
  73. } else if (anagrams.size() == largest_group_size) {
  74. // keep track of this anagram key
  75. result_keys.emplace(key);
  76. } else {
  77. // this anagram key starts a new biggest group
  78. set<string> new_anagram_result_keys;
  79. new_anagram_result_keys.emplace(key);
  80.  
  81. // get rid of the existing keyset and
  82. // start a new keyset with this key.
  83. //
  84. // ie. anagrams_map currently:
  85. // {act -> {tac, act},
  86. // {abe -> {bea, bae},
  87. // {deer -> {reed, eder}}
  88. //
  89. // result_key_set currently:
  90. // {act, abe, deer} since each group has size 2.
  91. //
  92. // we process a new word: cat.
  93. // {act -> {tac, act, cat},
  94. // {abe -> {bea, bae},
  95. // {deer -> {reed, eder}}
  96. //
  97. // our new result key set should just be
  98. // {act} of size 3.
  99. //
  100.  
  101. result_keys = move(new_anagram_result_keys);
  102. largest_group_size = anagrams.size();
  103. }
  104. }
  105. }
  106.  
  107.  
  108. if (argc != 2) {
  109. cerr << "Usage: ./anagramfinder <dictionary file>" << endl;
  110. return 1;
  111. }
  112. if (largest_group_size < 2) {
  113. cout << "No anagrams found." << endl;
  114. return 0;
  115. }
  116. cout << "Max anagrams: " << largest_group_size << endl;
  117.  
  118. vector<vector<string>> output;
  119. for (const auto &key : result_keys) {
  120. auto &group = anagrams_map[key];
  121. // sort the words in this group of anagrams
  122. sort(group.begin(), group.end());
  123. output.emplace_back(group);
  124. }
  125.  
  126. // sort the overall groups
  127. sort(output.begin(), output.end(), [](const vector<string> &a, const vector<string> &b) {
  128. return a[0] < b[0];
  129. });
  130.  
  131. for (unsigned int i = 0; i < output.size(); ++i) {
  132. const auto &group = output[i];
  133. if (i != 0) {
  134. cout << "" << endl;
  135. }
  136. for (const auto &word : group) {
  137. cout << word << endl;
  138. }
  139. }
  140. fs.close();
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement