Submission #1000117

# Submission time Handle Problem Language Result Execution time Memory
1000117 2024-06-16T16:49:41 Z vjudge1 Type Printer (IOI08_printer) C++17
90 / 100
836 ms 262144 KB
#include "bits/stdc++.h"

using namespace std;

#define int long long 

const int sz = 32 * 2e5 + 5;
const int inf = 1e18;

int trie[sz][26];
int depth[sz];
int leaf[sz];
int nxt = 0, cnt = 0;
char ch[sz];
vector<int> adj[sz];
vector<char> res;
int n;

struct Trie
{
    void add(string s) 
    {
        int root = 0;
        for(int i = 0; i < s.size(); i++)
        {
            int child = s[i] - 'a';
            if (!trie[root][child]) 
            {
                trie[root][child] = ++nxt;
                adj[root].push_back(trie[root][child]);
                ch[trie[root][child]] = s[i];
            }
            root = trie[root][child];
            depth[root] = max(depth[root], (int)s.size() - i);
        }
        leaf[root] = true;
    }
    void dfs(int node) 
    {
        if (node) res.push_back(ch[node]);
        if (leaf[node]) res.push_back('P'), cnt++;
        for(int to : adj[node]) dfs(to);
        if (cnt != n) res.push_back('-');
    }
};

void solve() 
{
    cin >> n;
    Trie tree;
    for(int i = 1; i <= n; i++) 
    {
        string s;
        cin >> s;
        tree.add(s);
    }
    for(int i = 0; i <= nxt; i++) sort(adj[i].begin(), adj[i].end(), [&](int a, int b)
    {
        return depth[a] < depth[b];
    });
    tree.dfs(0);
    cout << res.size() << endl;
    for(char c : res) cout << c << endl;
}

signed main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    // cin >> t;
    for(int i = 1; i <= t; i++) solve();
}

Compilation message

printer.cpp: In member function 'void Trie::add(std::string)':
printer.cpp:24:26: 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]
   24 |         for(int i = 0; i < s.size(); i++)
      |                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 59 ms 150608 KB Output is correct
2 Correct 57 ms 150612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 150740 KB Output is correct
2 Correct 59 ms 150608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 150612 KB Output is correct
2 Correct 56 ms 150608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 150612 KB Output is correct
2 Correct 53 ms 150616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 150876 KB Output is correct
2 Correct 66 ms 151644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 152364 KB Output is correct
2 Correct 74 ms 152904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 157012 KB Output is correct
2 Correct 175 ms 164432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 166864 KB Output is correct
2 Correct 97 ms 154076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 191692 KB Output is correct
2 Correct 836 ms 245196 KB Output is correct
3 Correct 530 ms 199060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 345 ms 182480 KB Output is correct
2 Runtime error 674 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -