Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. // #include "new_delete.hpp"
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <unordered_map>
  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 nh1(std::string str)
  46. {
  47.     // empty input
  48.     if (str.empty()) { return str; }
  49.  
  50.     // create vector to hold string rotations
  51.     auto perms = std::vector<std::string>();
  52.     perms.reserve(str.size());
  53.     perms.push_back(str);
  54.  
  55.     // create string rotations
  56.     for (std::size_t i = 1; i < str.size(); ++i)
  57.     {
  58.         std::rotate(str.begin(), str.begin() + 1, str.end());
  59.         perms.push_back(str);
  60.     }
  61.  
  62.     // return best (least) string
  63.     auto min_it = std::min_element(perms.begin(), perms.end());
  64.     return *min_it;
  65. }*/
  66.  
  67. std::string nh2(const std::string_view sv)
  68. {
  69.     // empty input
  70.     if (sv.empty()) { return {}; }
  71.  
  72.     // create strings
  73.     auto best_str = std::string(sv);
  74.     auto dup_str = std::string(sv);
  75.     dup_str.append(sv);
  76.  
  77.     // compare strings
  78.     for (std::size_t i = 1; i < sv.size(); ++i)
  79.     {
  80.         auto slide_view = std::string_view(dup_str.c_str() + i, sv.size());
  81.         if (slide_view < best_str) { best_str = slide_view; }
  82.     }
  83.  
  84.     // return best (least) string
  85.     return best_str;
  86. }
  87.  
  88. int main()
  89. {
  90.     // get filename
  91.     std::cout << "Enter a filename: ";
  92.     auto filename = std::string();
  93.     std::cin >> filename;
  94.  
  95.     // read words from file
  96.     auto t1 = Timer("include IO");
  97.     auto input_words = read_words(filename);
  98.     auto t2 = Timer("without IO");
  99.  
  100.     // sort words into necklaces
  101.     auto map = std::unordered_map<std::string, std::vector<std::string>>();
  102.     // map.reserve(180000);
  103.     for (auto& word : input_words)
  104.     {
  105.         auto nh = nh2(word);
  106.         map[nh].push_back(std::move(word));
  107.     }
  108.  
  109.     // get key with 4 permutations
  110.     auto key4 = std::string();
  111.     for (auto& [key, value] : map) { if (value.size() == 4) { key4 = key; break; } }
  112.  
  113.     // output results
  114.     if (key4.empty()) { std::cout << "No set of 4 necklace strings found." << std::endl; }
  115.     else
  116.     {
  117.         std::cout << "Key4: " << key4 << '\n';
  118.         std::cout << "Values:";
  119.         for (auto& val : map[key4]) { std::cout << '\n' << val; }
  120.         std::cout << '\n' << std::endl;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement