Submission #1108333

# Submission time Handle Problem Language Result Execution time Memory
1108333 2024-11-03T19:37:56 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
244 ms 218956 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
//typedef long long ll;
#define int long long
typedef pair<int, int> pii;
typedef tuple<int, int, int> t3i;
#define rep(a, b, c) for(int a = (int)b ; a < (int)c ; a++)
#define repi(a, b, c) for(int a = (int)b ; a >= (int)c ; a--)
#define ff first
#define ss second
#define pb push_back

//const int oo = 2e9;
const int oo = 4e18;
const vector<int> new_node = vector<int>(26, -1); // lista padrao pra novos nos

class Trie {
    public:
        vector<vector<int>> adj;
        vector<int> max_depth;
        vector<bool> str_terms;

        Trie() {
            adj.pb(new_node);
            str_terms.pb(false);
        }

        void add_str(string &s) {
            int idx = 0, no = 0;

            while (idx < s.size()) {
                if (adj[no][s[idx] - 'a'] != -1) no = adj[no][s[idx] - 'a'];
                else {
                    adj[no][s[idx] - 'a'] = adj.size();
                    no = adj.size();
                    adj.pb(new_node);
                    str_terms.pb(false);
                }

                idx++;
            }

            str_terms[no] = true;
        }

        void process_depth() {
            max_depth = vector<int>(adj.size(), 0);
            vector<vector<int>> depth(adj.size(), new_node);

            repi(no, adj.size()-1, 0) {
                int mai = 0;
                rep(i, 0, 26) {
                    if (adj[no][i] == -1) depth[no][i] = 0;
                    else depth[no][i] = 1 + max_depth[adj[no][i]]; 

                    mai = max(mai, depth[no][i]);
                }

                max_depth[no] = mai;
            }
        }
};

vector<char> ans;
int n, printados = 0;
Trie tri;

void dfs(int no) {
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    int d, c;

    // como todas as strs sao distintas, nao vao ter varias que terminam no msm lugar
    if (tri.str_terms[no]) {
        ans.pb('P');
        printados++;
    }

    rep(i, 0, 26) {
        if (tri.adj[no][i] == -1) continue;

        pq.push({tri.max_depth[tri.adj[no][i]], i});
    }

    while (!pq.empty()) {
        tie(d, c) = pq.top();
        pq.pop();

        // digita o caracter e continua
        ans.pb(c + 'a');
        dfs(tri.adj[no][c]);
    }

    // apaga o caracter se ainda tem que printar alguma coisa
    if (printados != n) ans.pb('-');
}

void solve() {
    string s;

    cin >> n;
    rep(i, 0, n) {
        cin >> s;
        tri.add_str(s);
    }

    tri.process_depth();
    dfs(0);

    cout << ans.size() << endl;
    for (char a : ans)
        cout << a << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    //int t;
    //cin >> t;
    //while (t--) 
        solve();

    return 0;
}

Compilation message

printer.cpp: In member function 'void Trie::add_str(std::string&)':
printer.cpp:34:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |             while (idx < s.size()) {
      |                    ~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 3 ms 2128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3664 KB Output is correct
2 Correct 6 ms 4492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 12552 KB Output is correct
2 Correct 34 ms 26628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 31492 KB Output is correct
2 Correct 14 ms 6540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 79600 KB Output is correct
2 Correct 213 ms 183524 KB Output is correct
3 Correct 106 ms 94636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 61648 KB Output is correct
2 Correct 244 ms 218956 KB Output is correct
3 Correct 131 ms 107248 KB Output is correct
4 Correct 223 ms 205356 KB Output is correct