답안 #1104204

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104204 2024-10-23T08:55:48 Z dzhoz0 Type Printer (IOI08_printer) C++17
20 / 100
122 ms 221468 KB
/*
    ghmt the cutie :3
          UwU
*/

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define INF 1e18
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>

const int MOD = 1'000'000'000 + 7;

void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    if (!name.empty())
    {
        freopen((name + ".INP").c_str(), "r", stdin);
        freopen((name + ".OUT").c_str(), "w", stdout);
    }
#endif
}

const int MAXNODES = 1e6;

struct Node {
    int child[26];
    int cnt;
    bool stop;
    Node() {
        memset(child, -1, sizeof(child));
        stop = 0;
        cnt = 0;
    }
};

Node trie[MAXNODES + 5];
int sz = 0;

void insert(string &s) {
    int pos = 0;
    for(char c : s) {
        if(trie[pos].child[c - 'a'] == -1) {
            trie[pos].child[c - 'a'] = ++sz;
        }
        pos = trie[pos].child[c - 'a']; 
        trie[pos].cnt++;
    }
    trie[pos].stop = 1;
}

vector<char> ops;

void traverse(int id) {
    if(trie[id].stop == 1) ops.push_back('P');
    vector<pair<int, char>> v;
    for(char c = 'a'; c <= 'z'; c++) {
        if(trie[id].child[c - 'a'] != -1) {
            // ops.push_back(c);
            // traverse(trie[id].child[c - 'a']);
            v.push_back({trie[trie[id].child[c - 'a']].cnt, c});
        }
    }
    sort(v.begin(), v.end());
    for(auto it : v) {
        ops.push_back(it.s);
        traverse(trie[id].child[it.s - 'a']);
    }
    ops.push_back('-');
}

void solve()
{
    int n; cin >> n;
    vector<string> a(n);
    for(string &s : a) {
        cin >> s;
        insert(s);
    }

    traverse(0);
    while(ops.back() != 'P') ops.pop_back();
    cout << ops.size() << '\n';
    for(char c : ops) cout << c << '\n';
}
signed main()
{
    setIO();
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}

Compilation message

printer.cpp: In function 'void setIO(std::string)':
printer.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen((name + ".INP").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen((name + ".OUT").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 219364 KB Output is correct
2 Correct 85 ms 219464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 106 ms 219468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 219464 KB Output is correct
2 Incorrect 94 ms 219404 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 219384 KB Output is correct
2 Correct 102 ms 219464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 90 ms 219720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 90 ms 219624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 82 ms 219980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 99 ms 220360 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 111 ms 221376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 122 ms 221468 KB Output isn't correct
2 Halted 0 ms 0 KB -