제출 #1363685

#제출 시각아이디문제언어결과실행 시간메모리
1363685vjudge1Type Printer (IOI08_printer)C++20
100 / 100
44 ms51156 KiB
/*
Author: Nhan Nguyen
School: University of Science, VNU-HCM
*/

#include <bits/stdc++.h>
#define sz(u) (int((u).size()))
#define all(x) (x).begin(), (x).end()
using namespace std;

const int maxNodes = int(5e5) + 10;

struct Node {
    int child[26];
    int exist, cnt;
};

struct Trie {
    int cur;
    bool longest[maxNodes];
    Node node[maxNodes];
    Trie() : cur(0) {
        memset(node[0].child, -1, sizeof(node[0].child));
        node[0].exist = node[0].cnt = 0;
    }
    int new_node() {
        cur++;
        memset(node[cur].child, -1, sizeof(node[cur].child));
        node[cur].exist = node[cur].cnt = 0;
        return cur;
    }
    void add_string(string s) {
        int pos = 0;
        for (auto f : s) {
            int c = f - 'a';
            if (node[pos].child[c] == -1) node[pos].child[c] = new_node();
            pos = node[pos].child[c];
            node[pos].cnt++;
        }
        node[pos].exist++;
    }
    void mark(string s) {
        int pos = 0;
        for (auto f : s) {
            int c = f - 'a';
            if (node[pos].child[c] == -1) return;
            pos = node[pos].child[c];
            longest[pos] = true;
        }
    }
};

int N;
vector<string> print;
vector<char> ans;
Trie tree;

void dfs(int pos) {
    for (int i = 0; i < tree.node[pos].exist; ++i) ans.push_back('P');
    int special = -1;
    for (int i = 0; i < 26; ++i) {
        int v = tree.node[pos].child[i];
        if (v != -1) {
            if (tree.longest[v]) {
                special = i;
            } else {
                ans.push_back(i + 'a');
                dfs(v);
                ans.push_back('-');
            }
        }
    }
    if (special != -1) {
        ans.push_back(special + 'a');
        dfs(tree.node[pos].child[special]);
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    if (fopen("inp.txt", "r")) {
        freopen("inp.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    cin >> N;
    string longest_string = "";
    for (int i = 1; i <= N; ++i) {
        string s;
        cin >> s;
        if (sz(longest_string) < sz(s)) longest_string = s;
        tree.add_string(s);
    }
    tree.mark(longest_string);
    dfs(0);
    cout << sz(ans) << '\n';
    for (auto c : ans) cout << c << '\n';
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

printer.cpp: In function 'int main()':
printer.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen("inp.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…