답안 #510214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
510214 2022-01-14T19:45:51 Z Yazan_Alattar Type Printer (IOI08_printer) C++14
0 / 100
1000 ms 76784 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);
        v.pb(s[cur]);
        cur = p[mask][cur];
        mask = to;
    }
    string have = "";
    for(int i = n - 1; i >= 0; --i){
        while(have.size() > s[i].size() || s[i].substr(0, (int)have.size()) != have) have.pop_back(), ans.pb('-');
        for(int j = have.size(); j < s[i].size(); ++j) have += s[i][j], ans.pb(s[i][j]);
        ans.pb('P');
    }
    for(int i = 0; i < have.size(); ++i) ans.pb('-');
    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:80:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for(int j = have.size(); j < s[i].size(); ++j) have += s[i][j], ans.pb(s[i][j]);
      |                                  ~~^~~~~~~~~~~~~
printer.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(int i = 0; i < have.size(); ++i) ans.pb('-');
      |                    ~~^~~~~~~~~~~~~
printer.cpp:72:27: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
   72 |         ll to = mask - (1 << cur);
      |                        ~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 37828 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 37804 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 38152 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 471 ms 58404 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 56 ms 76556 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 104 ms 76580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 245 ms 76672 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 689 ms 76784 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1074 ms 33532 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1073 ms 26360 KB Time limit exceeded
2 Halted 0 ms 0 KB -