답안 #935218

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
935218 2024-02-28T20:37:05 Z asdasdqwer Type Printer (IOI08_printer) C++14
20 / 100
1000 ms 38096 KB
#include <bits/stdc++.h>
using namespace std;

struct Node {
    char l;
    map<char,int> child;
};

vector<Node> trie(500001);
int last = 1;

void insWord(string &s) {
    int idx = 0;
    for (auto &x:s) {
        if (trie[idx].child.find(x) == trie[idx].child.end()) {
            trie[idx].child[x] = last;
            trie[last].l = x;
            last++;
        }
        idx = trie[idx].child[x];
    }
}

struct Operation {
    char chr;
    bool isPrint = false;
    bool isBack = false;
};

vector<Operation> out;

void traversal(int index) {
    bool foundOne = false;
    for (auto [chr, idx]:trie[index].child) {
        Operation op;op.chr = chr;
        out.push_back(op);
        traversal(idx);
        op.isBack = true;
        out.push_back(op);
        foundOne = true;
    }

    if (!foundOne) {
        Operation op;op.isPrint=true;
        out.push_back(op);
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;cin>>n;
    vector<string> v(n);
    for (auto &x:v)cin>>x;
    trie[0].l = ' ';

    for (auto x:v) {
        insWord(x);
    }

    // string s = "";
    // for (auto &x:v) {
    //     if (x.size() > s.size()) {
    //         s = x;
    //     }
    // }

    vector<Operation> vv;

    for (auto &s:v) {
        int index = 0;

        for (auto &x:s) {
            int nxt;
            for (auto [chr, idx] : trie[index].child) {
                if (chr != x) {
                    Operation op;op.chr=chr;
                    out.push_back(op);
                    traversal(idx);
                    op.isBack = true;
                    out.push_back(op);
                }

                else {
                    nxt = idx;
                }
            }

            Operation op;op.chr=x;
            out.push_back(op);
            index = nxt;
        }

        if (vv.size() == 0 || out.size() < vv.size()) {
            vv = out;
        }

        out.clear();
    }

    out = vv;

    Operation op;op.isPrint=true;
    out.push_back(op);

    cout<<out.size()<<"\n";
    for (auto &x:out) {
        if (x.isBack) {
            cout<<"-\n";
        }

        else if (x.isPrint) {
            cout<<"P\n";
        }

        else {
            cout<<x.chr<<"\n";
        }
    }
}

Compilation message

printer.cpp: In function 'void traversal(int)':
printer.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |     for (auto [chr, idx]:trie[index].child) {
      |               ^
printer.cpp: In function 'int main()':
printer.cpp:76:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |             for (auto [chr, idx] : trie[index].child) {
      |                       ^
printer.cpp:76:46: warning: 'nxt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |             for (auto [chr, idx] : trie[index].child) {
      |                                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 27740 KB Output is correct
2 Correct 9 ms 27740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27736 KB Output is correct
2 Correct 8 ms 27740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 27992 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 27740 KB Output is correct
2 Incorrect 7 ms 27740 KB didn't print every word
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 27736 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 96 ms 28252 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1096 ms 29468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1059 ms 31960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1024 ms 38096 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1063 ms 36052 KB Time limit exceeded
2 Halted 0 ms 0 KB -