Advertisement
Guest User

Untitled

a guest
May 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <unordered_map>
  5.  
  6. const std::string DICTIONARY_PATH = "input.txt";
  7. const std::string REQUESTS_PATH = "req.txt";
  8. const std::string OUTPUT_PATH = "output.txt";
  9.  
  10. class StringsStore
  11. {
  12. public:
  13. void AddString(const std::string& str);
  14. bool FindString(const std::string& str) const;
  15.  
  16. private:
  17. struct Node
  18. {
  19. Node() : finish(false) {}
  20. bool finish;
  21. std::unordered_map<char, Node> children;
  22. } root_;
  23. };
  24.  
  25. void StringsStore::AddString(const std::string& str)
  26. {
  27. Node * current = &root_;
  28.  
  29. for (char s : str)
  30. {
  31. if (current->children.find(s) == current->children.end())
  32. current->children.insert(std::make_pair(s, Node()));
  33. current = &current->children[s];
  34. }
  35.  
  36. current->finish = true;
  37. }
  38.  
  39. bool StringsStore::FindString(const std::string& str) const
  40. {
  41. const Node* current = &root_;
  42. for (char s : str)
  43. {
  44. const auto& child = current->children.find(s);
  45. if (child == current->children.end())
  46. return false;
  47.  
  48. current = &child->second;
  49. }
  50.  
  51. return current->finish;
  52. }
  53.  
  54. namespace StringsStoreHelper
  55. {
  56. StringsStore ReadFromFile(const std::string& path)
  57. {
  58. StringsStore store;
  59. std::ifstream infile(path);
  60. std::string line;
  61. while (std::getline(infile, line))
  62. store.AddString(line);
  63.  
  64. return store;
  65. }
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71. StringsStore store = StringsStoreHelper::ReadFromFile(DICTIONARY_PATH);
  72.  
  73. std::ifstream reqfile(REQUESTS_PATH);
  74. std::ofstream outfile(OUTPUT_PATH);
  75.  
  76. std::string line;
  77. while (std::getline(reqfile, line))
  78. outfile << store.FindString(line) << "\n";
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement