답안 #920316

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
920316 2024-02-02T12:48:01 Z Alihan_8 Type Printer (IOI08_printer) C++17
0 / 100
19 ms 3544 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    vector <string> s(n);
    for ( auto &x: s ) cin >> x;
    sort(all(s));
    auto f = [&](auto &u, auto &v){
        int i = 0;
        for (; i < min((int)u.size(), (int)v.size()); i++ ){
            if ( u[i] != v[i] ) break;
        }
        return i;
    };
    vector <int> pf(n);
    for ( int i = 1; i < n; i++ ){
        pf[i] = pf[i - 1] + f(s[i], s[i - 1]);
    }
    auto q = [&](int i){
        return pf[i] + (i + 1 < n ? f(s[0], s[i + 1]) + pf[n - 1] - pf[i + 1] : 0);
    };
    auto p = [&](int i){
        return pf[n - 1] - pf[i] + (i > 0 ? f(s[n - 1], s[i - 1]) + pf[i - 1] : 0);
    };
    int opt = 0, j = 0;
    for ( int i = 0; i < n; i++ ){
        int t = max(q(i), p(i));
        t += s[i].size();
        if ( chmax(opt, t) ){
            j = i;
        }
    }
    vector <string> a;
    if ( q(j) >= p(j) ){
        for ( int i = j; i >= 0; i-- ){
            a.pb(s[i]);
        }
        for ( int i = j + 1; i < n; i++ ){
            a.pb(s[i]);
        }
    } else{
        for ( int i = j; i < n; i++ ){
            a.pb(s[i]);
        }
        for ( int i = j - 1; i >= 0; i-- ){
            a.pb(s[i]);
        }
    }
    reverse(all(a));
    vector <char> op;
    for ( int i = 0; i < n; i++ ){
        if ( i > 0 ){
            int t = f(a[i - 1], a[i]), d = (int)a[i - 1].size() - t;
            while ( d-- ){
                op.pb('-');
            }
            for ( int j = t; j < a[i].size(); j++ ){
                op.pb(a[i][j]);
            }
        } else{
            for ( auto &x: a[i] ){
                op.pb(x);
            }
        } op.pb('P');
    }
    cout << op.size() << ln;
    for ( auto &x: op ){
        cout << x << ln;
    }

    cout << '\n';
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:88:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |             for ( int j = t; j < a[i].size(); j++ ){
      |                              ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 600 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 856 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 1744 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 3544 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 3540 KB Expected EOF
2 Halted 0 ms 0 KB -