Advertisement
ivnikkk

Untitled

Feb 1st, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #pragma GCC optimize("03")
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "bits/stdc++.h"
  4. //#include "geometry.h"
  5. //#include "data_structure.h"
  6. using namespace std;
  7. using namespace chrono;
  8. #define all(a) a.begin(), a.end()
  9. #define allr(a) a.rbegin(), a.rend()
  10. mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
  11. typedef long long ll;
  12. typedef long double ld;
  13.     struct Node {
  14.         ll go[10];
  15.         set<ll> used;
  16.         ll cnt = 0;
  17.     };
  18.     vector<Node> pref;
  19.     void init() {
  20.         pref.push_back(Node());
  21.     }
  22.     void add(ll x) {
  23.         ll cur_v = 0;
  24.         vector<ll> sub;
  25.         while (x) {
  26.             sub.push_back(x % 10);
  27.             x /= 10;
  28.         }
  29.         for (ll i = sub.size() - 1; i >= 0;--i) {
  30.             ll push = sub[i];
  31.             if (pref[cur_v].go[push] == 0) {
  32.                 pref.push_back(Node());
  33.                 pref[cur_v].go[push] = pref.size() - 1;
  34.                 pref[cur_v].used.insert(push);
  35.             }
  36.             cur_v = pref[cur_v].go[push];
  37.         }
  38.         pref[cur_v].cnt++;
  39.     }
  40.     vector<ll> mas[18];
  41.     void dfs(ll cur, ll num, ll dec) {
  42.         if (pref[cur].cnt) {
  43.             for (ll i = 0; i < pref[cur].cnt; i++) {
  44.                 mas[dec].push_back(num);
  45.             }
  46.         }
  47.         for (auto it = pref[cur].used.begin(); it != pref[cur].used.end(); it++) {
  48.             ll k = *it;
  49.             dfs(pref[cur].go[k], num * 10 + k, dec+1);
  50.         }
  51.     }
  52.  
  53. signed main() {
  54. #ifdef _DEBUG
  55.     freopen("input.txt", "r", stdin);
  56.     freopen("output.txt", "w", stdout);
  57. #endif
  58.     srand(time(NULL));
  59.     ios_base::sync_with_stdio(false);
  60.     cin.tie(nullptr);
  61.     cout.tie(nullptr);
  62.     //cout << fixed << setprecision(8)
  63.     init();
  64.     ll n;
  65.     cin >> n;
  66.     for (ll i = 0; i < n; i++) {
  67.         ll num;
  68.         cin >> num;
  69.         add(num);
  70.     }
  71.     dfs(0, 0, 0);
  72.     for (ll i = 0; i < 18; i++) {
  73.         if (mas[i].size()) {
  74.             for (ll j = 0; j < mas[i].size(); j++) {
  75.                 cout << mas[i][j] << ' ';
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement