제출 #632870

#제출 시각아이디문제언어결과실행 시간메모리
632870van_hoangType Printer (IOI08_printer)C++17
100 / 100
174 ms214936 KiB
#include "bits/stdc++.h"

using namespace std;

#ifndef LOCAL
#define debug(...) 86
#endif

const int maxN = 1e6 + 5;
const int maxC = 26;

struct node {
    int end, fl;
    node *child[maxC];

    node() {
        end = 0;
        for (int i = 0; i < maxC; ++i) child[i] = NULL;
    }
};

int nxt;
node nodes[maxN];

node *newnode() {
    return &nodes[nxt++];
}

vector<char> res;
struct trie {
    node *root;

    trie() {
        root = newnode();
    }

    void insert(node *n, string &s, int pos, int f) {
        n->fl = f;
        if (pos == (int)s.size()) {
            n->end = 1;
            return;
        }
        if (!n->child[s[pos] - 'a']) {
            n->child[s[pos] - 'a'] = newnode();
        }
        insert(n->child[s[pos] - 'a'], s, pos + 1, f);
    }

    void solve(node *n) {
        if (n->end) {
            res.emplace_back('P');
        }
        int pend = -1;
        for (int i = 0; i < maxC; ++i) {
            if (!n->child[i]) continue;
            if (n->child[i]->fl) {
                pend = i;
                continue;
            }
            res.emplace_back(char(i + 'a'));
            solve(n->child[i]);
            res.emplace_back('-');
        }
        if (pend != -1) {
            res.emplace_back(char(pend + 'a'));
            solve(n->child[pend]);
        }
    }
};

trie T;
string mx;
int N;

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    
    cin >> N;

    for (int i = 0; i < N; ++i) {
        string s; cin >> s;
        T.insert(T.root, s, 0, 0);
        if ((int)s.size() > (int)mx.size()) {
            mx = s;
        }
    }
    T.insert(T.root, mx, 0, 1);
    T.solve(T.root);

    cout << (int)res.size() << '\n';
    for (int i = 0; i < (int)res.size(); ++i) {
        cout << res[i] << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...