답안 #1004657

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004657 2024-06-21T11:39:24 Z NoLove Type Printer (IOI08_printer) C++14
100 / 100
120 ms 109772 KB
/**
 *    author : Lăng Trọng Đạt
 *    created: 21-06-2024 
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;

const int MAXN = 1e6 + 5;
int g[MAXN];
int nxt[MAXN][28], d[MAXN], max_d[MAXN];
int stop[MAXN];
int n, sz = 0;

void insert(string& s) {
    int v = 0;
    for (char c : s) {
        if (!nxt[v][c - 'a']) nxt[v][c - 'a'] = ++sz;
        v = nxt[v][c - 'a'];
    }
    stop[v]++;
}

vector<char> ans;
void dfs(int v) {
    int hi = -1;
    FOR(i, 0, 25) {
        if (nxt[v][i] && (hi == -1 or 
            max_d[nxt[v][i]] > max_d[nxt[v][hi]])) hi = i;
    }
    // db(v, hi, (char)(hi + 'a'))
    FOR(i, 0, 25) {
        if (nxt[v][i] && i != hi) {
            // cout << (char)(i + 'a') << "\n";
            ans.push_back((char)(i + 'a'));
            dfs(nxt[v][i]);
            if (n) {
                ans.push_back('-');

                // cout << "-\n";
            }
        }
    }

    while (stop[v]) stop[v]--, ans.push_back('P'), n--;
    if (hi != -1) {
        ans.push_back((char)(hi + 'a'));
        // cout << (char)(hi + 'a') << "\n";
        dfs(nxt[v][hi]);
        if (n)
                ans.push_back('-');
            // cout << "-\n";
    }
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("hi.inp", "r")) {
        freopen("hi.inp", "r", stdin);
       freopen("hi.out", "w", stdout);
    } 

    string s;
    cin >> n;
    vector<string> t;
    FOR(i, 1, n) {
        cin >> s;   
        t.push_back(s);
        insert(s);
    }
    for (int i = sz; i >= 0; i--) {
        int cnt_chill = 0;
        FOR(c, 0, 25) 
            if (nxt[i][c]) {
                cnt_chill++;
                mx(max_d[i], max_d[nxt[i][c]] + 1);
            }
    }
    dfs(0);

    db(ans.size())
    cout << ans.size() << "\n";
    int cnt = 0;
    for (char c : ans) {
        cout << c << "\n";
        if (c == 'P') cnt++;
    }

    string x = "";
    vector<string> hi;
    for (char c : ans) {
        if (c == '-') {
            x.pop_back();
        }
        else if (c == 'P') hi.push_back(x);
        else x.push_back(c);
    }
    db(ans)
    // sort(all(hi));
    // sort(all(t));
    // db(hi == t)
    // for (auto s : t)
    //     cout << s << "\n";
}

Compilation message

printer.cpp: In function 'void dfs(long long int)':
printer.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
printer.cpp:43:5: note: in expansion of macro 'FOR'
   43 |     FOR(i, 0, 25) {
      |     ^~~
printer.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
printer.cpp:48:5: note: in expansion of macro 'FOR'
   48 |     FOR(i, 0, 25) {
      |     ^~~
printer.cpp: In function 'int32_t main()':
printer.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
printer.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 1, n) {
      |     ^~~
printer.cpp:18:31: warning: unnecessary parentheses in declaration of 'c' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
printer.cpp:89:9: note: in expansion of macro 'FOR'
   89 |         FOR(c, 0, 25)
      |         ^~~
printer.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
printer.cpp:76:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |        freopen("hi.out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 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 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 472 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2140 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 6744 KB Output is correct
2 Correct 15 ms 14172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 16864 KB Output is correct
2 Correct 7 ms 4860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 41640 KB Output is correct
2 Correct 105 ms 92372 KB Output is correct
3 Correct 54 ms 49484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 33232 KB Output is correct
2 Correct 120 ms 109772 KB Output is correct
3 Correct 62 ms 55880 KB Output is correct
4 Correct 100 ms 103748 KB Output is correct