제출 #1363684

#제출 시각아이디문제언어결과실행 시간메모리
1363684vjudge1Type Printer (IOI08_printer)C++17
100 / 100
65 ms51316 KiB
#include <bits/stdc++.h>
#define len(a) int((a).size())
#define ll long long
#define name "W"
using namespace std;

void fastio(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

void file(){
    if(fopen(name ".inp", "r")){
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
}

const int N = 25e3 + 7;
const int mxNODE = 5e5 + 7;
int n, cnt_word = 0;
string a[N];
vector <char> ans;

void inp(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
}

bool cmp(string &x, string &y){
    return len(x) < len(y);
}

struct Trie{
    struct Node{
        int child[26];
        int exist;
    } nodes[mxNODE];

    int cur;
    Trie(): cur(0){
        memset(nodes[0].child, -1, sizeof nodes[0].child);
        nodes[0].exist = 0;
    }

    int new_node(){
        cur++;
        memset(nodes[cur].child, -1, sizeof nodes[cur].child);
        nodes[cur].exist = 0;
        return cur;
    }

    void add_str(string s){
        int pos = 0;
        for(char x : s){
            int c = x - 'a';
            if(nodes[pos].child[c] == -1)
                nodes[pos].child[c] = new_node();
            pos = nodes[pos].child[c];
        }
        nodes[pos].exist++;
    }

    void deal(int pos, int u, int v){
        for(int i = 1; i <= nodes[pos].exist; i++){
            ans.push_back('P');
            cnt_word++;
        }
        for(int i = 0; i < 26; i++){
            if(u == v && char(i + 'a') == a[n][u]) continue;
            if(nodes[pos].child[i] == -1) continue;
            ans.push_back(char(i + 'a'));
            deal(nodes[pos].child[i], u, v + 1);
        }
        if(u == v){
            if(u < len(a[n])){
                if(nodes[pos].child[a[n][u] - 'a'] != -1){
                    ans.push_back(a[n][u]);
                    deal(nodes[pos].child[a[n][u] - 'a'], u + 1, v + 1);
                }
            }
        }
        if(pos != 0 && cnt_word < n)
            ans.push_back('-');
    }
};

Trie p;

void solve(){
    inp();
    sort(a + 1, a + 1 + n, cmp);
    for(int i = 1; i <= n; i++){
        p.add_str(a[i]);
    }
    p.deal(0, 0, 0);
    cout << len(ans) << '\n';
    for(char x : ans){
        cout << x << '\n';
    }
}

int main(){
    fastio();
    file();
    solve();
    return 0;
}

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

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