제출 #1360106

#제출 시각아이디문제언어결과실행 시간메모리
1360106herominhsteveType Printer (IOI08_printer)C++20
20 / 100
31 ms39808 KiB
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NAME"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9 + 7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}

void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    if (fopen(FNAME".inp","r")){
        freopen(FNAME".inp","r",stdin);
        freopen(FNAME".out","w",stdout);
    }
}

struct Node {
    Node* child[26];
    int cnt, exist;
    bool isEnd;

    Node() {
        for (int i = 0; i < 26; i++) {
            child[i] = nullptr;
        }
        cnt = exist = 0;
        isEnd = false;
    }
};

namespace Trie {
    int cur;
    Node* root = new Node();

    void addString(string str) {
        Node* p = root;
        for (char x : str) {
            int c = x - 'a';
            if (p->child[c] == nullptr) {
                p->child[c] = new Node();
            }
            p = p->child[c];
            p->cnt++;
        }
        p->exist++;
        p->isEnd = true;
    }

    bool findString(string str) {
        Node* p = root;
        for (char x : str) {
            int c = x - 'a';
            if (p->child[c] == nullptr) {
                return false;
            }
            p = p->child[c];
        }
        return (p->exist > 0);
    }

    bool deleteStringDo(Node* p, string str, int cur) {
        if (cur != (int)str.size()) {
            int c = str[cur] - 'a';
            bool isDeleted = deleteStringDo(p->child[c], str, cur + 1);
            if (isDeleted) {
                p->child[c] = nullptr;
            }
        } else {
            p->exist--;
        }

        if (p != root) {
            p->cnt--;
            if (p->cnt == 0) {
                delete p;
                return true;
            }
        }
        return false;
    }

    void deleteString(string str) {
        if (!findString(str)) {
            return;
        }
        deleteStringDo(root, str, 0);
    }
};

int n;
vector<string> str;

void init(){
    cin>>n;
    str.resize(n);
    for (auto &x : str) cin>>x;
}

string res;
int numPrinted = 0;

void dfs(Node *p, char curChar){
    if (curChar != '$') res.push_back(curChar);
    if (p->isEnd){
        res.push_back('P');
        numPrinted++;
    }

    vector<pair<int, char>> nxt;
    for (int i = 0; i < 26; i++) if (p->child[i] != nullptr){
        nxt.emplace_back(p->child[i]->cnt, char(i + 'a'));
    }
    sort(allof(nxt));

    for (const auto &[sz, c] : nxt)
        dfs(p->child[c - 'a'], c);

    if (numPrinted < n) res.push_back('-');
}

void sol(){
    for (const auto &x : str) Trie::addString(x);
    dfs(Trie::root, '$');
    cout<<res.size()<<el;
    for (char c : res) cout<<c<<el;
}

int main(){
    setup();
    init();
    sol();
}

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

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