Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. #define int long long
  10. #define p_b push_back
  11.  
  12. struct vertex
  13. {
  14.     vector<int> out; // исходящие ребра
  15.     bool leaf;  //флаг есть ли конец слова
  16.     char cur_sym;//символ входящего ребра
  17.     string word;
  18.     int inp;
  19. };
  20.  
  21. struct correct
  22. {
  23.     string correct_word;
  24.     int end_mistakes = 0;
  25. };
  26. struct chip
  27. {
  28.     int index_ver;
  29.     int letter;
  30.     int mistakes = 0;
  31. };
  32.  
  33. vector<correct> listt;
  34. vector<vertex> bor;
  35. vector<chip> first;
  36. vector<chip> second;
  37.  
  38. template<typename T> //заголовочный файл!
  39. class Slovar {
  40. public:
  41.     void push(const string& word, const T& data);
  42.     bool check(const string& word);
  43. };
  44.  
  45. //добавление строки
  46. void push(const string& word, int position, int bor_index) { //position позиция в слове, bor_index место где мы в дереве бора
  47.  
  48.     //проверка на лист(конец слова)
  49.     if (position == word.size()) {
  50.         bor[bor_index].leaf = true;
  51.         bor[bor_index].word = word;
  52.         return;
  53.     }
  54.  
  55.     //перебор всех рёбер
  56.     for (int i = 0; i < bor[bor_index].out.size(); i++) {
  57.         if (word[position] == bor[bor[bor_index].out[i]].cur_sym) {
  58.             push(word, position + 1, bor[bor_index].out[i]);
  59.             return;
  60.         }
  61.     }
  62.  
  63.     //добавление недостоющей вершины
  64.     vertex new_vertex;
  65.     new_vertex.cur_sym = word[position];
  66.     new_vertex.leaf = false;
  67.     bor.p_b({ new_vertex });
  68.     bor[bor_index].out.p_b(bor.size() - 1);
  69.     bor[bor.size() - 1].inp = bor_index;//??
  70.     push(word, position + 1, bor.size() - 1);
  71. }
  72.  
  73. int prov = 0;
  74. //поиск слова в словаре(боре)
  75. void search(const string & word, int position, int bor_index)
  76. {
  77.     if ((position == word.size()) && (bor[bor_index].leaf == true)) {
  78.         prov = 1;
  79.         return;
  80.     }
  81.  
  82.     //смотрим все ребра
  83.     for (int i = 0; i < bor[bor_index].out.size(); i++) {
  84.         if (word[position] == bor[bor[bor_index].out[i]].cur_sym) {
  85.             search(word, position + 1, bor[bor_index].out[i]);
  86.             return;
  87.         }
  88.     }
  89.     prov = -1;
  90. }
  91. int k = 0;
  92. int p = 0;
  93.  
  94. //исправление ошибок
  95. void typos(const string& word, vector<chip>& first, int max_typos) {
  96.  
  97.     for (int i = 0; i < first.size(); i++) { // по всем вершинам 1 слоя, пусть нам дан этот массив
  98.         for (int j = 0; j < bor[first[i].index_ver].out.size(); j++) { // по всем ребрам каждого элемента 1 ого массива
  99.             chip new_chip; // создаем новую вишку 2ого уровня
  100.             new_chip.index_ver = bor[first[i].index_ver].out[j]; // вершина этой фишки исходящие ребро j(из выходящих ребер) из вершины i
  101.             new_chip.letter = first[i].letter + 1;// позиция в слове сместится на 1
  102.  
  103.             if (word[first[i].letter] != bor[bor[first[i].index_ver].out[j]].cur_sym) { // если прочитанная буква была норм, то оставим ошибки на месте иначе прибавим 1
  104.                 new_chip.mistakes = first[i].mistakes + 1;
  105.             }
  106.             else new_chip.mistakes = first[i].mistakes;
  107.            
  108.             if ((new_chip.mistakes <= max_typos) && (new_chip.letter == word.size()) && (bor[new_chip.index_ver].leaf == true)) //проверяем есть ли в этой новой фишке уонец всего
  109.             {
  110.                 listt[k].correct_word = bor[new_chip.index_ver].word;
  111.                 listt[k].end_mistakes = new_chip.mistakes;
  112.                 k++;
  113.             }
  114.             second.p_b({ new_chip }); // пишем все такие вишки в массив второго уровня
  115.             p++;
  116.             if (p == bor.size()) {
  117.                 return;
  118.             }
  119.         }
  120.     }
  121.     typos(word, second, max_typos);
  122.     return;
  123. }
  124. signed main()
  125. {
  126.     bor.resize(1);
  127.     bor[0].leaf = false;
  128.     listt.resize(1);
  129.  
  130.     first.resize(1);
  131.     first[0].index_ver = 0;
  132.     first[0].letter = 0;
  133.     first[0].mistakes = 0;
  134.  
  135.     second.resize(0);
  136.  
  137.     string test = "privetik";
  138.     string test1 = "privw";
  139.     string test22 = "hasvddik";
  140.     push(test, 0, 0);
  141.     push(test1, 0, 0);
  142.     for (int i = 0; i < bor.size(); i++) {
  143.         cout << bor[i].cur_sym << '\n';
  144.     }
  145.     search(test1, 0, 0);
  146.     typos(test22, first, test22.size()/2);
  147.     for (int i = 0; i < listt.size(); i++) {
  148.         cout << listt[i].correct_word << listt[i].end_mistakes << '\n';
  149.     }
  150.     //cout << prov << '\n';
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement