답안 #114527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114527 2019-06-01T16:07:15 Z popovicirobert Type Printer (IOI08_printer) C++14
100 / 100
183 ms 99296 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

struct Trie {
    Trie *son[26];
    int mx, cnt;

    Trie() {
        memset(son, 0, sizeof(son));
        mx = cnt = 0;
    }
};

Trie *root = new Trie;

void add(Trie *nod, string &str, int p) {
    if(p < str.size()) {
        char ch = str[p] - 'a';
        if(nod -> son[ch] == NULL) {
            nod -> son[ch] = new Trie;
        }
        add(nod -> son[ch], str, p + 1);
        nod -> mx = max(nod -> mx, nod -> son[ch] -> mx + 1);
    }
    else {
        nod -> cnt++;
    }
}

string sol;

void dfs(Trie *nod) {
    int id = -1;
    for(int ch = 0; ch < 26; ch++) {
        if(nod -> son[ch] == NULL) {
            continue;
        }
        if(nod -> son[ch] -> mx + 1 == nod -> mx) {
            id = ch;
        }
    }


    while(nod -> cnt > 0) {
        sol.push_back('P');
        nod -> cnt--;
    }

    for(int ch = 0; ch < 26; ch++) {
        if(id != ch && nod -> son[ch] != NULL) {
            sol.push_back(ch + 'a');
            dfs(nod -> son[ch]);
            sol.push_back('-');
        }
    }

    if(id > -1) {
        sol.push_back(id + 'a');
        dfs(nod -> son[id]);
        sol.push_back('-');
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    string str;
    for(i = 0; i < n; i++) {
        cin >> str;
        add(root, str, 0);
    }

    dfs(root);

    while(sol.back() == '-') {
        sol.pop_back();
    }

    cout << sol.size() << "\n";
    for(auto it : sol) {
        cout << it << "\n";
    }

    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

printer.cpp: In function 'void add(Trie*, std::__cxx11::string&, int)':
printer.cpp:23:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(p < str.size()) {
        ~~^~~~~~~~~~~~
printer.cpp:25:25: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(nod -> son[ch] == NULL) {
                         ^
printer.cpp:26:26: warning: array subscript has type 'char' [-Wchar-subscripts]
             nod -> son[ch] = new Trie;
                          ^
printer.cpp:28:26: warning: array subscript has type 'char' [-Wchar-subscripts]
         add(nod -> son[ch], str, p + 1);
                          ^
printer.cpp:29:49: warning: array subscript has type 'char' [-Wchar-subscripts]
         nod -> mx = max(nod -> mx, nod -> son[ch] -> mx + 1);
                                                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1792 KB Output is correct
2 Correct 6 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6016 KB Output is correct
2 Correct 26 ms 12544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 14736 KB Output is correct
2 Correct 11 ms 3328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 36448 KB Output is correct
2 Correct 163 ms 83348 KB Output is correct
3 Correct 87 ms 42896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 28568 KB Output is correct
2 Correct 183 ms 99296 KB Output is correct
3 Correct 98 ms 48580 KB Output is correct
4 Correct 168 ms 93640 KB Output is correct