Submission #520063

# Submission time Handle Problem Language Result Execution time Memory
520063 2022-01-28T09:30:25 Z HaYoungJoon Type Printer (IOI08_printer) C++14
30 / 100
28 ms 32120 KB
#include <bits/stdc++.h>

using namespace std;

int cost[100001], trie[100001][26],timer = 0;
int n;
vector<int> adj[100001];
bool special[100001], finish[100001];
char label[100001];
vector<char> res;

void addString(string s) {
    int cur = 0;
    for (char c: s) {
        int val = c - 'a';
        if (!trie[cur][val]) {
            trie[cur][val] = ++timer;
            adj[cur].emplace_back(timer);
            label[timer] = c;
        }
        cur = trie[cur][val];
    }
    special[cur] = 1;
}

void DFS_1(int u) {
    for (int v: adj[u]) {
        DFS_1(v);
        if (cost[v]) cost[u] += cost[v] + 1;
    }
    if (cost[u]) cost[u] += adj[u].size();
    if (!cost[u])
        if (special[u]) cost[u] = 1;
}

bool cmp(const int &A, const int &B) {
    return cost[A] < cost[B];
}

void DFS_2(int u) {
    sort(adj[u].begin(),adj[u].end(),cmp);
    if (special[u]) {
        res.emplace_back('P');
        n--;
    }
    for (int v: adj[u]) {
        if (!cost[v]) continue;
        res.emplace_back(label[v]);
        DFS_2(v);
    }
    if (n) res.emplace_back('-');
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    string s;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        addString(s);
    }
    DFS_1(0);
    DFS_2(0);
    cout << res.size() << '\n';
    for (char c: res) cout << c << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3660 KB Output is correct
2 Incorrect 4 ms 3880 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 6220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 11840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 32120 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 31768 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -