trafik

Untitled

Mar 21st, 2022
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <set>
  5. #include <deque>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <iomanip>
  12. #include <fstream>
  13. #include <unordered_map>
  14. #define all(v) v.begin(), v.end()
  15. #define ll long long
  16. #define ld long double
  17. #define lb lower_bound
  18. #define ub upper_bound
  19. #define len(v) (ll)v.size()
  20. #define last(v) (ll)v[len(v) - 1]
  21. using namespace std;
  22. const ll inf = 1e9;
  23. const ll MAXN = 1e6 + 1;
  24. const ll MAXM = 1e5 + 1;
  25. const int logn = 29;
  26. template<class T>
  27. istream& operator>>(istream& in, vector<T>& a) {
  28.     for (auto& i : a)
  29.         in >> i;
  30.     return in;
  31. }
  32. template<class T>
  33. ostream& operator<<(ostream& out, vector<T>& a) {
  34.     for (auto& i : a)
  35.         out << i << " ";
  36.     return out;
  37. }
  38.  
  39. struct node {
  40.     node* next[26];
  41.     short int strings;
  42.     node() {
  43.         for (int i = 0; i < 26; i++) {
  44.             next[i] = nullptr;
  45.         }
  46.         strings = 0;
  47.     }
  48. };
  49.  
  50. node* root = new node();
  51. void add(string s) {
  52.     node* curv = root;
  53.     for (int i = 0; i < len(s); ++i) {
  54.         char c = s[i];
  55.         if (curv->next[c - 'a'] == nullptr) {
  56.             curv->next[c - 'a'] = new node();
  57.         }
  58.         curv = curv->next[c - 'a'];
  59.     }
  60.     curv->strings++;
  61. }
  62.  
  63.  
  64. string s;
  65. string ans = "";
  66. vector<string> sl;
  67. string cur_str = "";
  68. int curi = 0;
  69. void write(node* v = root) {
  70.     for (int i = 0; i < v->strings; i++) {
  71.         while (s[curi] == '.') {
  72.             ans += '.';
  73.             ++curi;
  74.         }
  75.         ans += cur_str;
  76.         while (s[curi] != '.') ++curi;
  77.     }
  78.  
  79.     for (int i = 0; i < 26; i++) {
  80.         if (v->next[i] != nullptr) {
  81.             cur_str.push_back('a' + i);
  82.             write(v->next[i]);
  83.             cur_str.pop_back();
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89.  
  90. void solve() {
  91.     cin >> s;
  92.     s += '.';
  93.     bool f = 0;
  94.     if (s[0] != '.') f = 1;
  95.     string cur = "";
  96.     for (int i = 0; i < len(s); ++i) {
  97.         if (s[i] == '.' && f) {
  98.             f = 0;
  99.             add(cur);
  100.             cur = "";
  101.         }
  102.         else if (s[i] != '.' && !f) {
  103.             f = 1;
  104.             cur += s[i];
  105.         }
  106.         else if (s[i] != '.' && f) cur += s[i];
  107.     }
  108.     while (s[curi] == '.' && curi < (len(s) - 1)) {
  109.         ans += '.';
  110.         ++curi;
  111.     }
  112.     write();
  113.     /*int sli = 0;
  114.     for (int i = 0; i < (len(s) - 1);) {
  115.         if (s[i] == '.') {
  116.             ans += '.';
  117.             ++i;
  118.             continue;
  119.         }
  120.         ans += sl[sli++];
  121.         while (s[i] != '.') ++i;
  122.     }*/
  123.     while (s[curi] == '.' && curi < (len(s) - 1)) {
  124.         ans += '.';
  125.         ++curi;
  126.     }
  127.     cout << ans;
  128. }
  129.  
  130. int main() {
  131.     ios::sync_with_stdio(false);
  132.     cin.tie(nullptr);
  133.     cout.tie(nullptr);
  134.  
  135.     solve();
  136.  
  137.     return 0;
  138. };
Advertisement
Add Comment
Please, Sign In to add comment