fork download
  1. #include <cstddef>
  2. #include <exception>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <limits>
  6. #include <string>
  7. #include <string_view>
  8. #include <unordered_map>
  9. #include <unordered_set>
  10.  
  11. class TripletWordAdapter
  12. {
  13. public:
  14. class TripletWordIterator
  15. {
  16. public:
  17. TripletWordIterator(const std::string_view word, const std::size_t position)
  18. : m_word(word)
  19. , m_position(position)
  20. {
  21. if (m_word.length() <= 4)
  22. throw std::logic_error("The word is too short to iterate through");
  23. }
  24.  
  25. std::pair<std::string_view, std::string_view> operator*() const
  26. {
  27. if (m_position + 4 > m_word.length())
  28. throw std::logic_error("Iterator is out of a valid range");
  29.  
  30. return std::pair(m_word.substr(m_position, 3),
  31. m_word.substr(m_position + 1, 3));
  32. }
  33.  
  34. TripletWordIterator& operator++()
  35. {
  36. ++m_position;
  37. if (m_position > m_word.length() - 3)
  38. throw std::logic_error("Iterator is out of a valid range");
  39.  
  40. return *this;
  41. }
  42.  
  43. bool operator!=(const TripletWordIterator other) const
  44. {
  45. return m_position != other.m_position;
  46. }
  47.  
  48. private:
  49. const std::string_view m_word;
  50. std::size_t m_position;
  51. };
  52.  
  53. TripletWordAdapter(std::string_view word)
  54. : m_word(word)
  55. {
  56. if (m_word.length() < 4)
  57. throw std::logic_error("Can't wrap a word too short to iterate through");
  58. }
  59.  
  60. TripletWordIterator begin() const { return TripletWordIterator(m_word, 0); }
  61. TripletWordIterator end() const
  62. {
  63. return TripletWordIterator(m_word, m_word.length() - 3);
  64. }
  65.  
  66. private:
  67. const std::string_view m_word;
  68. };
  69.  
  70. class WordGraphSolver
  71. {
  72. public:
  73. void ProcessWord(const std::string_view word)
  74. {
  75. for (auto [from, to] : TripletWordAdapter(word)) {
  76. if (from.compare(to) < 0)
  77. std::swap(from, to);
  78.  
  79. if (++m_edges[std::string(from) + ' ' + std::string(to)] != 1)
  80. continue;
  81.  
  82. // A new edge have appeared, meaning a new vertex has been added to a
  83. // graph
  84. m_vertices.emplace(from);
  85. m_vertices.emplace(to);
  86. }
  87. }
  88.  
  89. void PrintResults() const
  90. {
  91. std::cout << m_vertices.size() << std::endl;
  92. std::cout << m_edges.size() << std::endl;
  93. for (const auto& [edge, weight] : m_edges)
  94. std::cout << edge << ' ' << weight << std::endl;
  95. }
  96.  
  97. private:
  98. std::unordered_set<std::string> m_vertices;
  99. std::unordered_map<std::string, std::uint32_t> m_edges;
  100. };
  101.  
  102. int
  103. main()
  104. {
  105. std::ifstream input("input.txt");
  106. input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  107.  
  108. WordGraphSolver solver;
  109. std::string line;
  110. while (std::getline(input, line))
  111. solver.ProcessWord(line);
  112.  
  113. solver.PrintResults();
  114. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:10: fatal error: 'string_view' file not found
#include <string_view>
         ^~~~~~~~~~~~~
1 error generated.
stdout
Standard output is empty