Advertisement
Sanlover

Untitled

Mar 30th, 2022
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class NODE
  8. {
  9. public:
  10.     bool eow;
  11.     NODE* ptrs[26];
  12.  
  13.     NODE()
  14.     {
  15.         eow = false;
  16.         for (short j = 0; j < 26; ++j)
  17.         {
  18.             ptrs[j] = nullptr;
  19.         }
  20.     }
  21.  
  22.     ~NODE()
  23.     {
  24.         eow = true;
  25.         for (int i = 0; i < 26; i++)
  26.         {
  27.             ptrs[i] = nullptr;
  28.         }
  29.     }
  30. };
  31.  
  32. using TrieTree = NODE*;
  33.  
  34. void init(TrieTree t)
  35. {
  36.     t = nullptr;
  37. }
  38.  
  39. void add(TrieTree& t, const string word, short i)
  40. {
  41.     if (!t)
  42.     {
  43.         t = new NODE;
  44.     }
  45.     if (static_cast<short>(word.length()) - 1 < i)
  46.     {
  47.         t->eow = true;
  48.     }
  49.     else
  50.     {
  51.         add(t->ptrs[word[i] - 'a'], word, i + 1);
  52.     }
  53. }
  54.  
  55. bool _all_ptrs_empty(TrieTree t)
  56. {
  57.     bool result = true;
  58.     int i = 0;
  59.     while (i < 26 && result)
  60.     {
  61.         if (t->ptrs[i])
  62.         {
  63.             result = false;
  64.         }
  65.         else
  66.         {
  67.             i++;
  68.         }
  69.     }
  70.     return result;
  71. }
  72.  
  73. bool del(TrieTree& t, const string word, short i) //сделать remove bool
  74. {
  75.     bool result = false;
  76.     if (t)
  77.     {
  78.         if (i <= static_cast<short>(word.length() - 1))
  79.         {
  80.             result = del(t->ptrs[word[i] - 'a'], word, i + 1);
  81.         }
  82.         else
  83.         {
  84.             t->eow = false;
  85.             if (_all_ptrs_empty(t))
  86.             {
  87.                 delete t;
  88.                 t = nullptr;
  89.             }
  90.             result = true;
  91.         }
  92.     }
  93.     return result;
  94. }
  95.  
  96. void print(TrieTree t, string word)
  97. {
  98.     if (t->eow)
  99.     {
  100.         cout << word << endl;
  101.     }
  102.     for (int i = 0; i < 26; ++i)
  103.     {
  104.         if (t->ptrs[i])
  105.         {
  106.             print(t->ptrs[i], word + static_cast<char>(i + 'a'));
  107.         }
  108.     }
  109. }
  110. // node - узел, word - слово которое собирается в трай дереве, phrase - фраза которую ищем в начале, count - количество
  111. // слов, которые имеют в начале себя phrase, phrasePos - позиция символа в phrase для сравнения с символами на разных узлах дерева
  112. void task(TrieTree node, const string& word, const string& phrase, size_t& count, const size_t& phrasePos)
  113. {
  114.     // Если слово закончилось, и наша позиция перешла или равна длине фразы, то мы её содержим, а значит подсчитываем и выводим
  115.     if (node->eow && phrasePos >= phrase.size())
  116.     {
  117.         count++;
  118.         cout << word << endl;
  119.     }
  120.     // Бежим по всем символам
  121.     for (int i = 0; i < 26; ++i)
  122.     {
  123.         // Если мы не пустые и ( либо позиция символа в фразе уже вышла за пределы длины [а значит слово уже входит], либо должны
  124.         // совпасть символы)
  125.         if (node->ptrs[i] && (phrasePos >= phrase.size() || static_cast<char>(i + 'a') == phrase[phrasePos]))
  126.         {   // Запускаем рекурсию с позицией в фразе+1
  127.             task(node->ptrs[i], word + static_cast<char>(i + 'a'), phrase, count, phrasePos + 1);
  128.         }
  129.     }
  130. }
  131.  
  132. void clear(TrieTree& t)
  133. {
  134.     for (int i = 0; i < 26; ++i)
  135.     {
  136.         if (t->ptrs[i])
  137.         {
  138.             clear(t->ptrs[i]);
  139.         }
  140.     }
  141.     delete t;
  142.     t = nullptr;
  143. }
  144.  
  145. class TRIE_tree
  146. {
  147. public:
  148.     TrieTree root;
  149.     TRIE_tree() { init(root); }
  150.  
  151.     TRIE_tree(string path)
  152.     {
  153.         init(root);
  154.         ifstream i_file(path);
  155.         string word = "";
  156.         while (!i_file.eof())
  157.         {
  158.             getline(i_file, word);
  159.             add_word(word);
  160.         }
  161.     }
  162.  
  163.     ~TRIE_tree();
  164.     void add_word(string word);
  165.     bool all_ptrs_empty(TrieTree t);
  166.     bool tree_empty(TrieTree t);
  167.     bool del_word(string word);
  168.     void clear_tree();
  169.     void print_tree();
  170. };
  171.  
  172.  
  173. int main()
  174. {
  175.     ifstream file("data_1.txt");
  176.     if (!file)
  177.     {
  178.         cout << "Error!\n";
  179.     }
  180.     else
  181.     {
  182.         TrieTree t = nullptr;
  183.         init(t);
  184.         string word;
  185.         while (!file.eof())
  186.         {
  187.             getline(file, word);
  188.             if (word.length())
  189.             {
  190.                 add(t, word, 0);
  191.             }
  192.         }
  193.         print(t, "");
  194.         cout << "--------\n";
  195.         size_t counter = 0;
  196.         task(t, "", "wo", counter, 0);
  197.         cout << "Amount: " << counter << endl;
  198.         //taskprint(t, "", false);
  199.         clear(t);
  200.     }
  201.     return 0;
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement