답안 #857804

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
857804 2023-10-07T02:24:32 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
92 ms 37196 KB
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;

#define N 200005
#define ALL(x) x.begin(), x.end()
vector<char> ans;
int cend2;

int n;
char s[21];
struct Trie
{
    int start = 0, end = 0, sz = 1;
    vector<tuple<int, char,Trie*>> sortbysz;
    
    Trie() {}

    void insert(char *s, int p = 0)
    {
        if (!*s) { ++end; return; }
        if (p == 1) ++start;
        char c = *s++;
        for (auto [sz, ch, n] : sortbysz)
            if (ch == c)
            { n->insert(s); return; }
        Trie *n = new Trie();
        sortbysz.emplace_back(1, c, n);
        n->insert(s);
    }

    void preprocess()
    {
        for (auto &[csz, c, n] : sortbysz)
        {
            n->preprocess();
            sz = max(sz, csz = n->sz+1);
        }
        sort(ALL(sortbysz));
    }

    void print()
    {
        while (end--)
        {
            ans.push_back('P');
            if (++cend2 == n) 
            {
                printf("%d\n", (int)ans.size());
                for (char c : ans) printf("%c\n", c);
                exit(0);
            }
        }
        for (auto [sz, c, n] : sortbysz)
        {
            ans.push_back(c);
            n->print();
        }
        ans.push_back('-');
    }
};


int main()
{
    scanf("%d", &n);
    Trie *t = new Trie();
    for (int i = 0; i < n; ++i)
    {
        scanf("%s", s);
        t->insert(s);
    }

    t->preprocess();
    t->print();

    return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 856 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2396 KB Output is correct
2 Correct 12 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 5716 KB Output is correct
2 Correct 5 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 13892 KB Output is correct
2 Correct 76 ms 31176 KB Output is correct
3 Correct 45 ms 16036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 10808 KB Output is correct
2 Correct 92 ms 37196 KB Output is correct
3 Correct 45 ms 18124 KB Output is correct
4 Correct 75 ms 35048 KB Output is correct