Raslboyy

111788

Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <unordered_set>
  5. #include <string>
  6. #include <list>
  7. #include <functional>
  8.  
  9. using namespace std;
  10.  
  11. const int N_A = 26;
  12.  
  13. class Node {
  14. public:
  15.     char letter;
  16.     int count = 0;
  17.     Node* way[26]{nullptr};
  18.     Node(char letter) : letter(letter) {};
  19. };
  20.  
  21. class Bor {
  22. public:
  23.     Node* root = new Node('-');
  24.     int size = 0;
  25.     list<string> res;
  26.     void Add(string str) {
  27.         Node* cur = root;
  28.         for (char ch : str) {
  29.             if (cur->way[ch - 'a'] != nullptr) {
  30.                 cur = cur->way[ch - 'a'];
  31.             }
  32.             else {
  33.                 cur->way[ch - 'a'] = new Node(ch);
  34.                 cur = cur->way[ch - 'a'];
  35.             }
  36.         }
  37.         cur->count++;
  38.         size++;
  39.     }
  40.     list<string>& get_res() {
  41.         print(root);
  42.         return res;
  43.     }
  44.     string cur_str = "";
  45.     void print(Node* cur) {
  46.         if (cur != root)
  47.             for (int i = 0; i < cur->count; i++) {
  48.                 res.emplace_back(cur_str);
  49.             }
  50.         for (auto v : cur->way) {
  51.             if (v != nullptr) {
  52.                 cur_str.push_back(v->letter);
  53.                 print(v);
  54.                 cur_str.pop_back();
  55.             }
  56.         }
  57.     };
  58. };
  59.  
  60.  
  61.  
  62. int main() {
  63.     ifstream fin("input.txt");
  64.     ofstream fout("output.txt");
  65.  
  66.     list<int> t;
  67.     int cur_t = 0; string s = "";
  68.     char ch;
  69.     Bor b;
  70.     while (fin >> ch) {
  71.         if (ch == '.') {
  72.             b.Add(s);
  73.             s = "";
  74.             cur_t++;
  75.         }
  76.         else {
  77.             if (s == "") t.emplace_back(cur_t);
  78.             cur_t = 0;
  79.             s += ch;
  80.         }
  81.     }
  82.     t.emplace_back(cur_t);
  83.     b.Add(s);
  84.     auto it_t = t.begin();
  85.     fout << string((*it_t), '.');
  86.     it_t++;
  87.     for (string& s : b.get_res()) {
  88.         fout << (s + string((*it_t), '.'));
  89.         it_t++;
  90.     }
  91.  
  92.  
  93.  
  94.     fin.close();
  95.     fout.close();
  96.     return 0;
  97. }
Add Comment
Please, Sign In to add comment