제출 #1366080

#제출 시각아이디문제언어결과실행 시간메모리
1366080vjudge1Type Printer (IOI08_printer)C++20
100 / 100
99 ms101208 KiB
#include<bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
#define endl '\n'
#define ll long long

int n;
string res;
int numPrints = 0;

struct node{
    bool type = false;
    int numbWords = 0;
    node *child[26];
};

node *createNode() {
    node *p = new node();

    for (int i = 0; i < 26; i++) {
        p->child[i] = NULL;
    }

    return p;
}

void addWord(node *root, string &s, bool ok) {
    node *p = root;

    for (char c : s) {
        if (p->child[c - 'a'] == NULL) {
            p->child[c - 'a'] = createNode();
        }

        p = p->child[c - 'a'];
        p->type = ok;
    }

    p->numbWords++;
}

void dfs(node* root) {
    for (int i = 1; i <= root->numbWords; i++) {
        res += 'P';

        numPrints++;
        if (numPrints == n) {
            cout << res.size() << endl;
            for (int i = 0; i < res.size(); i++) cout << res[i] << endl;
            exit(0);
        }
    }

    char chLast = '.';
    for (int i = 0; i < 26; i++) {
        if (root->child[i] != NULL && root->child[i]->type == true) {
            chLast = (char) (i + 'a');
        }
    }


    for (int i = 0; i < 26; i++) {
        if (root->child[i] == NULL || (char) (i + 'a') == chLast) {
            continue;
        }

        res += (char) (i + 'a');
        dfs(root->child[i]);
        res += '-';
    }

    if (chLast != '.') {
        res += chLast;
        dfs(root->child[(chLast - 'a')]);
    }
}

void solve() {
    cin >> n;

    vector<string> s(n);

    int pos;
    int lenMax = 0;

    for (int i = 0; i < n; i++) {
        cin >> s[i];
        if (lenMax < s[i].size()) {
            pos = i;
            lenMax = s[i].size();
        }
    }

    node *root = createNode();

    for (int i = 0; i < n; i++) {
        if (i != pos) {
            addWord(root, s[i], false);
        }
    }

    addWord(root, s[pos], true);

    dfs(root);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
//    #ifndef ONLINE_JUDGE
//    freopen("inp.txt","r",stdin);
//    freopen("out.txt","w",stdout);
//    #endif

    solve();

    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…