Submission #858953

# Submission time Handle Problem Language Result Execution time Memory
858953 2023-10-09T12:59:38 Z ntkphong Type Printer (IOI08_printer) C++14
0 / 100
38 ms 37484 KB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 100010;

struct node {
    int w_count;
    int child[26];
};

int n;
vector<string> s;
vector<node> tree;
vector<int> mark(mxN * 20);
vector<char> answer;
int id_max;
int cnt = 0;

void new_node() {
    node x;
    x.w_count = 0;
    for(int i = 0; i < 26; i ++) x.child[i] = -1;
    tree.push_back(x);
}

void init() {
    new_node();
}

void add(string s, int type) {
    int idx = 0;
    for(int i = 0; i < s.size(); i ++) {
        int w = s[i] - 'a';
        if(tree[idx].child[w] == -1) {
            new_node();
            tree[idx].child[w] = tree.size() - 1;
        }
        idx = tree[idx].child[w];
        mark[idx] = type;
    }
    tree[idx].w_count ++ ;
}

void _dfs(int u) {
    for(int i = 1; i <= tree[u].w_count; i ++) {
        answer.push_back('P');
        cnt ++ ;
    }
    
    if(cnt == n) {
        cout << answer.size() << "\n";
        for(auto c : answer) cout << c;
    
        exit(0);
    }
    
    for(int i = 0; i < 26; i ++) {
        int v = tree[u].child[i];
        if(v == -1) continue ;
        if(!mark[v]) {
            answer.push_back((char)(i + 'a'));
            _dfs(v);
            answer.push_back('-');
        }
    }
    
    for(int i = 0; i < 26; i ++) {
        int v = tree[u].child[i];
        if(v == -1) continue ;
        if(mark[v]) {
            answer.push_back((char)(i + 'a'));
            _dfs(v);
            answer.push_back('-');
        }
    }
}

int main() {
#define taskname ""

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

    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n;
    
    s.resize(n + 1);
    init();
    
    for(int i = 1; i <= n; i ++) {
        cin >> s[i];
        if(s[i].size() > s[id_max].size()) {
            id_max = i;
        }
    }
    
    for(int i = 1; i <= n; i ++) {
        if(i == id_max) continue ;
        add(s[i], 0);
    }
    
    add(s[id_max], 1);
    _dfs(0);
    return 0;
}
 

Compilation message

printer.cpp: In function 'void add(std::string, int)':
printer.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int i = 0; i < s.size(); i ++) {
      |                    ~~^~~~~~~~~~
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(taskname".inp", "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(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 8280 KB Line "tptttykduyvxjbzhqupP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 8284 KB Line "eejzatjmnqxctnP--------------h...-yerxP----labfaryosskugbkiuffdP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 8284 KB Line "hjxgqkP------iupqiqP------rhpq...------------wPfxlmwfirlgbdevjdP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 8284 KB Line "bhvdxrpthaP----------edczvdgoy...--------------xomsgennpdlurnmvP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 8284 KB Line "abbblauqkfdP----------edP--svh...----ooP--ulwzP----eynorwrbizaiP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 9256 KB Line "aPaisxlhagqbjwbP-------------b...----------zP-cclviwgdudcybahuwP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11788 KB Line "aPacaoP---izjpfrpvhbP---------...------silP---zuknicjtukmwmlddzP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 16720 KB Line "aPadmeP---ejuixzggakP---------...----vP-wshP---gPkwzakqubhstcdqP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 37484 KB Line "aPaadfP--iznyxP-----nqjP----bo...------------nhbvobjbxuwlhzwchfP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 24012 KB Line "aPaPbakyloxqrjP----------chfaP...vpiP--------jbfbP---jtearnhdjeP" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -