Submission #1108332

# Submission time Handle Problem Language Result Execution time Memory
1108332 2024-11-03T19:31:22 Z vjudge1 Type Printer (IOI08_printer) C++17
90 / 100
267 ms 262144 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, depth;
        vector<int> max_depth;
        vector<bool> str_terms;

        Trie() {
            adj.pb(new_node);
            depth.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);
                    depth.pb(new_node);
                    str_terms.pb(0);
                }

                idx++;
            }

            str_terms[no] = true;
        }

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

            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<string> 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(string(1, 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 (string 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:35: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]
   35 |             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 848 KB Output is correct
2 Correct 4 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4300 KB Output is correct
2 Correct 7 ms 6000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16060 KB Output is correct
2 Correct 35 ms 33972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 43036 KB Output is correct
2 Correct 13 ms 10176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 102548 KB Output is correct
2 Correct 267 ms 228736 KB Output is correct
3 Correct 140 ms 119384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 82328 KB Output is correct
2 Runtime error 239 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -