Advertisement
Sanlover

Untitled

Jan 25th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <map>
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9. string word;
  10. int counter;
  11. Node* left;
  12. Node* right;
  13. };
  14.  
  15. class WordTree
  16. {
  17. Node* head;
  18. void add_method(const string& _Word, Node*& _Node)
  19. {
  20. if (_Node == nullptr)
  21. {
  22. _Node = new Node;
  23. _Node->word = _Word;
  24. _Node->counter = 1;
  25. _Node->left = _Node->right = nullptr;
  26. }
  27. else
  28. {
  29. if (_Node->word > _Word)
  30. add_method(_Word, _Node->right);
  31. else if (_Node->word < _Word)
  32. add_method(_Word, _Node->left);
  33. else
  34. _Node->counter++;
  35. }
  36. }
  37. bool is_found_method(const string& _Word, Node* _Node)
  38. {
  39. if (_Node == nullptr)
  40. return false;
  41.  
  42. if (_Node->word == _Word)
  43. return true;
  44. else
  45. {
  46. if (_Node->word > _Word)
  47. is_found_method(_Word, _Node->right);
  48. else if (_Node->word < _Word)
  49. is_found_method(_Word, _Node->left);
  50. }
  51. };
  52.  
  53. public:
  54. WordTree()
  55. {
  56. head = nullptr;
  57. }
  58. void add(const string& _Word)
  59. {
  60. add_method(_Word, head);
  61. }
  62.  
  63.  
  64. bool is_found(const string& _Word)
  65. {
  66. return is_found_method(_Word, head);
  67. }
  68. };
  69.  
  70. int main()
  71. {
  72. WordTree a;
  73.  
  74. ifstream input("input.txt");
  75. ifstream addition("compare_with.txt");
  76. if (!input.is_open() || !addition.is_open())
  77. return 0;
  78.  
  79. while(!input.eof())
  80. {
  81. string tmp;
  82. getline(input, tmp);
  83. a.add(tmp);
  84. }
  85. input.close();
  86.  
  87. map<string,int> compare_with;
  88. while(!addition.eof())
  89. {
  90. string tmp;
  91. getline(input, tmp);
  92. compare_with[tmp]++;
  93. }
  94. addition.close();
  95.  
  96. ofstream output("output.txt");
  97. for(map<string,int>::iterator it = compare_with.begin(); it!= compare_with.end(); ++it)
  98. {
  99. if(a.is_found(it->first))
  100. // Delete function
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement