Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <unordered_set>
- #include <string>
- #include <list>
- #include <functional>
- using namespace std;
- const int N_A = 26;
- class Node {
- public:
- char letter;
- int count = 0;
- Node* way[26]{nullptr};
- Node(char letter) : letter(letter) {};
- };
- class Bor {
- public:
- Node* root = new Node('-');
- int size = 0;
- list<string> res;
- 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->count++;
- size++;
- }
- list<string>& get_res() {
- print(root);
- return res;
- }
- string cur_str = "";
- void print(Node* cur) {
- if (cur != root)
- for (int i = 0; i < cur->count; i++) {
- res.emplace_back(cur_str);
- }
- for (auto v : cur->way) {
- if (v != nullptr) {
- cur_str.push_back(v->letter);
- print(v);
- cur_str.pop_back();
- }
- }
- };
- };
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- list<int> t;
- int cur_t = 0; string s = "";
- char ch;
- Bor b;
- while (fin >> ch) {
- if (ch == '.') {
- b.Add(s);
- s = "";
- cur_t++;
- }
- else {
- if (s == "") t.emplace_back(cur_t);
- cur_t = 0;
- s += ch;
- }
- }
- t.emplace_back(cur_t);
- b.Add(s);
- auto it_t = t.begin();
- fout << string((*it_t), '.');
- it_t++;
- for (string& s : b.get_res()) {
- fout << (s + string((*it_t), '.'));
- it_t++;
- }
- fin.close();
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment