Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("03")
- #define _CRT_SECURE_NO_WARNINGS
- #include "bits/stdc++.h"
- //#include "geometry.h"
- //#include "data_structure.h"
- using namespace std;
- using namespace chrono;
- #define all(a) a.begin(), a.end()
- #define allr(a) a.rbegin(), a.rend()
- mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
- typedef long long ll;
- typedef long double ld;
- struct Node {
- ll go[10];
- set<ll> used;
- ll cnt = 0;
- };
- vector<Node> pref;
- void init() {
- pref.push_back(Node());
- }
- void add(ll x) {
- ll cur_v = 0;
- vector<ll> sub;
- while (x) {
- sub.push_back(x % 10);
- x /= 10;
- }
- for (ll i = sub.size() - 1; i >= 0;--i) {
- ll push = sub[i];
- if (pref[cur_v].go[push] == 0) {
- pref.push_back(Node());
- pref[cur_v].go[push] = pref.size() - 1;
- pref[cur_v].used.insert(push);
- }
- cur_v = pref[cur_v].go[push];
- }
- pref[cur_v].cnt++;
- }
- vector<ll> mas[18];
- void dfs(ll cur, ll num, ll dec) {
- if (pref[cur].cnt) {
- for (ll i = 0; i < pref[cur].cnt; i++) {
- mas[dec].push_back(num);
- }
- }
- for (auto it = pref[cur].used.begin(); it != pref[cur].used.end(); it++) {
- ll k = *it;
- dfs(pref[cur].go[k], num * 10 + k, dec+1);
- }
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- srand(time(NULL));
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- //cout << fixed << setprecision(8)
- init();
- ll n;
- cin >> n;
- for (ll i = 0; i < n; i++) {
- ll num;
- cin >> num;
- add(num);
- }
- dfs(0, 0, 0);
- for (ll i = 0; i < 18; i++) {
- if (mas[i].size()) {
- for (ll j = 0; j < mas[i].size(); j++) {
- cout << mas[i][j] << ' ';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement