Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. //#include "new_delete.hpp"
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <unordered_map>
  9.  
  10. struct Timer
  11. {
  12.     using clock_type = std::chrono::high_resolution_clock;
  13.     using time_point_type = std::chrono::time_point<clock_type>;
  14.     using duration_type = std::chrono::duration<double>;
  15.  
  16.     time_point_type start;
  17.     std::string name;
  18.  
  19.     Timer(const std::string& name) : name(name) { start = clock_type::now(); }
  20.     ~Timer()
  21.     {
  22.         auto end = clock_type::now();
  23.         auto duration = duration_type(end - start);
  24.         auto ms = duration.count() * 1000.0f;
  25.         std::cout << "Timer '" << name << "' took " << ms << "ms " << std::endl;
  26.     }
  27. };
  28.  
  29. std::vector<std::string> read_words(const std::string& file_path)
  30. {
  31.     auto words = std::vector<std::string>();
  32.     auto ifs = std::ifstream(file_path);
  33.     auto line = std::string();
  34.  
  35.     while (ifs)
  36.     {
  37.         std::getline(ifs, line);
  38.         words.push_back(line);
  39.     }
  40.  
  41.     return words;
  42. }
  43.  
  44. std::string necklace_hash(std::string str)
  45. {
  46.     if (str.empty()) { return str; }
  47.  
  48.     auto perms = std::vector<std::string>();
  49.     perms.reserve(str.size());
  50.     perms.push_back(str);
  51.  
  52.     for (std::size_t i = 1; i < str.size(); ++i)
  53.     {
  54.         std::rotate(str.begin(), str.begin() + 1, str.end());
  55.         perms.push_back(str);
  56.     }
  57.  
  58.     auto min_it = std::min_element(perms.begin(), perms.end());
  59.     return *min_it;
  60. }
  61.  
  62. int main()
  63. {
  64.     // get filename
  65.     std::cout << "Enter a filename: ";
  66.     auto filename = std::string();
  67.     std::cin >> filename;
  68.  
  69.     // read words from file
  70.     auto t1 = Timer("include IO");
  71.     auto input_words = read_words(filename);
  72.     auto t2 = Timer("without IO");
  73.  
  74.     // sort words into necklaces
  75.     auto map = std::unordered_map<std::string, std::vector<std::string>>();
  76.     for (auto& word : input_words)
  77.     {
  78.         auto nh = necklace_hash(word);
  79.         map[nh].push_back(std::move(word));
  80.     }
  81.  
  82.     // get key with 4 permutations
  83.     auto key4 = std::string();
  84.     for (auto& [key, value] : map) { if (value.size() == 4) { key4 = key; break; } }
  85.  
  86.     // output results
  87.     if (key4.empty()) { std::cout << "No set of 4 necklace strings found." << std::endl; }
  88.     else
  89.     {
  90.         std::cout << "Key4: " << key4 << '\n';
  91.         std::cout << "Values:";
  92.         for (auto& val : map[key4]) { std::cout << '\n' << val; }
  93.         std::cout << '\n' << std::endl;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement