Advertisement
Guest User

Untitled

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