Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <string>
- using namespace std;
- class Node {
- public:
- char let;
- bool f;
- Node* way[26]{ nullptr };
- Node(char let, int f = 0) : let(let), f(f) {};
- };
- class Bor {
- public:
- Node* root = new Node('-');
- int count = 0;
- int maxx = 0;
- void Add(string str) {
- Node* cur = root;
- for (char ch : str) {
- if (cur->way[ch - 'a'] != nullptr) {
- cur = cur->way[ch - 'a'];
- }
- else {
- cur->way[ch - 'a'] = new Node(ch);
- cur = cur->way[ch - 'a'];
- }
- }
- cur->f = 1;
- }
- void get(Node* cur) {
- if (cur->f)
- maxx = max(maxx, count);
- for (Node* v : cur->way) {
- if (v != nullptr) {
- count+=1*v->f;
- get(v);
- count -= 1 * v->f;
- }
- }
- }
- int get_max() {
- get(root);
- return maxx;
- }
- };
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int n; fin >> n;
- Bor br; string str;
- for (int i = 0; i < n; i++) {
- fin >> str;
- br.Add(str);
- }
- fout << br.get_max() << endl;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment