Raslboyy

111730

Oct 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Node {
  9. public:
  10.     char let;
  11.     bool f;
  12.     Node* way[26]{ nullptr };
  13.     Node(char let, int f = 0) : let(let), f(f) {};
  14. };
  15.  
  16. class Bor {
  17. public:
  18.     Node* root = new Node('-');
  19.     int count = 0;
  20.     int maxx = 0;
  21.     void Add(string str) {
  22.         Node* cur = root;
  23.         for (char ch : str) {
  24.             if (cur->way[ch - 'a'] != nullptr) {
  25.                 cur = cur->way[ch - 'a'];
  26.             }
  27.             else {
  28.                 cur->way[ch - 'a'] = new Node(ch);
  29.                 cur = cur->way[ch - 'a'];
  30.             }
  31.         }
  32.         cur->f = 1;
  33.     }
  34.     void get(Node* cur) {
  35.         if (cur->f)
  36.             maxx = max(maxx, count);
  37.         for (Node* v : cur->way) {
  38.             if (v != nullptr) {
  39.                 count+=1*v->f;
  40.                 get(v);
  41.                 count -= 1 * v->f;
  42.             }
  43.         }
  44.     }
  45.     int get_max() {
  46.         get(root);
  47.         return maxx;
  48.     }
  49. };
  50.  
  51. int main() {
  52.     ifstream fin("input.txt");
  53.     ofstream fout("output.txt");
  54.  
  55.     int n; fin >> n;
  56.     Bor br; string str;
  57.     for (int i = 0; i < n; i++) {
  58.         fin >> str;
  59.         br.Add(str);
  60.     }
  61.     fout << br.get_max() << endl;
  62.  
  63.     fin.close();
  64.     fout.close();
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment