Submission #745595

# Submission time Handle Problem Language Result Execution time Memory
745595 2023-05-20T14:38:34 Z Perl32 Type Printer (IOI08_printer) C++14
100 / 100
182 ms 90956 KB
/**
 *    author:  Perl32
 *    created: 19.05.2023 17:00:04
**/

#ifdef LOCAL
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) (int) x.size()
#define all(x) x.begin(), x.end()
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef complex<double> cmpl;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template<typename K, typename V> using normal_unordered_map = gp_hash_table<K, V>;
template<typename T> using normal_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

const int INF = (int) 1e9 + 1e3;
const ll INFL = (ll) 1e18 + 1e6;
#ifdef LOCAL
    mt19937 tw(9450189);
#else
    mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;

ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

struct Trie {
    struct Node {
        unordered_map<char, int> next;
        bool done = false;
        int cnt = 0;
    };

    vector<Node> tree;

    Trie() {
        tree.emplace_back();
    }

    bool contain(int v, char ch) {
        return tree[v].next.find(ch) != tree[v].next.end();
    }

    int add(int v, char ch, bool terminal) {
        if (!contain(v, ch)) {
            tree[v].next[ch] = szof(tree);
            tree.emplace_back();
        }
        int to = tree[v].next[ch];
        if (terminal) {
            tree[to].cnt++;
        }
        return to;
    }

    int go(int v, char ch) {
        if (!contain(v, ch)) {
            return -1;
        }
        return tree[v].next[ch];
    }

    void get() {
        vector<char> ans;
        function<void(int)> dfs = [&](int u) {
            for (int i = 0; i < tree[u].cnt; ++i) {
                ans.push_back('P');
            }
            for (auto [ch, to] : tree[u].next) {
                if (!tree[to].done) {
                    ans.push_back(ch);
                    dfs(to);
                    ans.push_back('-');
                }
            }
            for (auto [ch, to] : tree[u].next) {
                if (tree[to].done) {
                    ans.push_back(ch);
                    dfs(to);
                }
            }
        };
        dfs(0);
        cout << szof(ans) << '\n';
        for (auto x : ans) {
            cout << x << '\n';
        }
    }
};

signed main() {
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("err.txt", "w", stderr);
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    Trie tree;
    string max = "";
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        int v = 0;
        for (int j = 0; j < szof(s); ++j) {
            v = tree.add(v, s[j], j == szof(s) - 1);
        }
        if (szof(max) < szof(s)) {
            swap(max, s);
        }
    }
    int v = 0;
    for (int i = 0; i < szof(max); ++i) {
        v = tree.go(v, max[i]);
        tree.tree[v].done = true;
    }
    tree.get();

#ifdef LOCAL
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (ll) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}

Compilation message

printer.cpp: In lambda function:
printer.cpp:86:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |             for (auto [ch, to] : tree[u].next) {
      |                       ^
printer.cpp:93:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   93 |             for (auto [ch, to] : tree[u].next) {
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 2 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1612 KB Output is correct
2 Correct 4 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5316 KB Output is correct
2 Correct 23 ms 11104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 12928 KB Output is correct
2 Correct 10 ms 3144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 33864 KB Output is correct
2 Correct 159 ms 76576 KB Output is correct
3 Correct 86 ms 38336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 24720 KB Output is correct
2 Correct 182 ms 90956 KB Output is correct
3 Correct 99 ms 43456 KB Output is correct
4 Correct 170 ms 85796 KB Output is correct