답안 #510223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
510223 2022-01-14T19:59:07 Z Yazan_Alattar Type Printer (IOI08_printer) C++14
10 / 100
1000 ms 76624 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

ll cost(string s, string t)
{
    string pref = "";
    for(int i = 0; i < min(s.size(), t.size()); ++i){
        if(s[i] != t[i]) break;
        pref += s[i];
    }
//    cout << s << " " << t << " " << s.size() - pref.size() + t.size() - pref.size() << endl;
    return s.size() - pref.size() + t.size() - pref.size();
}

vector <char> ans;
string s[M];
ll n, dp[M][20], last[M], p[M][20];

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; ++i) cin >> s[i];
    sort(s, s + n);
    for(int i = 0; i < M; ++i) for(int j = 0; j < n; ++j) dp[i][j] = inf;
    for(int i = 0; i < n; ++i) dp[(1 << i)][i] = n + s[i].size();
    for(int mask = 1; mask < (1 << n); ++mask){
        for(int last = 0; last < n; ++last) if((mask >> last) & 1){
            for(int i = 0; i < n; ++i){
                if((mask >> i) & 1) continue;
                int to = mask + (1 << i);
                if(dp[mask][last] + cost(s[last], s[i]) < dp[to][i]){
                    dp[to][i] = dp[mask][last] + cost(s[last], s[i]);
                    p[to][i] = last;
                }
            }
        }
    }
    ll mn = inf, mask = (1 << n) - 1, cur;
    for(int i = 0; i < n; ++i){
        if(dp[mask][i] < mn){
            mn = dp[mask][i];
            cur = i;
        }
    }
    cout << mn << endl;
    vector <string> v;
    while(mask){
        ll to = mask - (1 << cur);
//        cout << cur << endl;
        v.pb(s[cur]);
        cur = p[mask][cur];
        mask = to;
    }
    string have = "";
    for(int i = n - 1; i >= 0; --i){
        while(have.size() > v[i].size() || v[i].substr(0, (int)have.size()) != have) have.pop_back(), ans.pb('-');
        for(int j = have.size(); j < v[i].size(); ++j) have += s[i][j], ans.pb(v[i][j]);
        ans.pb('P');
    }
    for(auto i : ans) cout << i << endl;
    return 0;
}

Compilation message

printer.cpp: In function 'll cost(std::string, std::string)':
printer.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   30 |     for(int i = 0; i < min(s.size(), t.size()); ++i){
      |                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:81:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for(int j = have.size(); j < v[i].size(); ++j) have += s[i][j], ans.pb(v[i][j]);
      |                                  ~~^~~~~~~~~~~~~
printer.cpp:72:27: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
   72 |         ll to = mask - (1 << cur);
      |                        ~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 37836 KB Output is correct
2 Correct 18 ms 37852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 37936 KB Output is correct
2 Incorrect 17 ms 37940 KB Expected EOF
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 38220 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 459 ms 58396 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 66 ms 76528 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 100 ms 76624 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 244 ms 76608 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 682 ms 76580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1087 ms 32156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1092 ms 26796 KB Time limit exceeded
2 Halted 0 ms 0 KB -