Submission #1360111

#TimeUsernameProblemLanguageResultExecution timeMemory
1360111herominhsteveType Printer (IOI08_printer)C++20
100 / 100
78 ms108008 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, len;
    bool isEnd;

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

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

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

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]->len, 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();
}

Compilation message (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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...