답안 #878565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878565 2023-11-24T18:08:32 Z kh0i Type Printer (IOI08_printer) C++17
100 / 100
88 ms 63680 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 25003;

struct Trie{
    struct Node{
        int child[26];
        int cnt, exist;
        bool marked;

        Node(){
            memset(child, -1, sizeof(child));
            cnt = exist = marked = 0;
        }
    };

    int cur = 0;
    vector<Node> node;

    void init(){
        cur = 0;
        node.push_back(Node());
    }

    int new_node(){
        ++cur;
        node.push_back(Node());
        return cur;
    }

    void mark_longest(string &s){
        int pos = 0;
        for(int i = 0; i < sz(s); ++i){
            int c = s[i] - 'a';
            pos = node[pos].child[c];
            node[pos].marked = 1;
        }
    }

    void add(string &s){
        int pos = 0;
        for(int i = 0; i < sz(s); ++i){
            int c = s[i] - 'a';
            if(node[pos].child[c] == -1)
                node[pos].child[c] = new_node();
            pos = node[pos].child[c];
            ++node[pos].cnt;
        }
        ++node[pos].exist;
    }

    void dfs(int cur, string &s){
        while(node[cur].exist--)
            s.push_back('P');

        int marked = -1;

        for(int i = 0; i < 26; ++i){
            if(node[cur].child[i] != -1){
                if(node[node[cur].child[i]].marked){
                    marked = i;
                    continue;
                }
                s.push_back(char(i + 'a'));
                dfs(node[cur].child[i], s);
            }
        }

        if(marked != -1){
            s.push_back(char(marked + 'a'));
            dfs(node[cur].child[marked], s);
        }

        s.push_back('-');
    }

    string calc(){
        string res = "";
        dfs(0, res);
        while(res.back() == '-')
            res.pop_back();
        return res;
    }
} trie;

int n;
string s[N];

void solve(){
    trie.init();
    cin >> n;
    string longest = "";
    for(int i = 1; i <= n; ++i){
        cin >> s[i];
        trie.add(s[i]);
        if(sz(longest) < sz(s[i]))
            longest = s[i];
    }

    trie.mark_longest(longest);
    string res = trie.calc();

    cout << sz(res) << '\n';
    for(char c : res)
        cout << c << '\n';
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message

printer.cpp: In function 'int32_t main()':
printer.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1372 KB Output is correct
2 Correct 2 ms 1804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2316 KB Output is correct
2 Correct 3 ms 3124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5780 KB Output is correct
2 Correct 11 ms 9040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10576 KB Output is correct
2 Correct 6 ms 3128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 32456 KB Output is correct
2 Correct 88 ms 62888 KB Output is correct
3 Correct 39 ms 33736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 17452 KB Output is correct
2 Correct 83 ms 63420 KB Output is correct
3 Correct 43 ms 32968 KB Output is correct
4 Correct 70 ms 63680 KB Output is correct