답안 #292428

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292428 2020-09-06T23:18:00 Z couplefire Type Printer (IOI08_printer) C++17
80 / 100
1000 ms 37460 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 25005

vector<pair<int, char>> adj[20*MAXN];
int dp1[MAXN*20], dp2[MAXN*20];
int ans[MAXN*20];
int n;
int cur = 0;
pair<int, char> nodes[MAXN*20];
string ts;
int cnt = 0;

void addWord(int v, int p, int index){
    if(index == ts.length()){
        adj[v].push_back({++cur, 'P'});
        adj[cur].push_back(nodes[v]);
        nodes[cur] = {cur, 'P'};
        return;
    }
    bool flag = false;
    for(auto u : adj[v]){
        if(p == u.first) continue;
        if(u.second == ts[index]){
            flag = true;
            addWord(u.first, v, index+1);
        }
    }
    if(!flag){
        adj[v].push_back({++cur, ts[index]});
        adj[cur].push_back(nodes[v]);
        nodes[cur] = {cur, ts[index]};
        addWord(cur, v, index+1);
    }
}

void calc(int v, int p){
    dp1[v] = dp2[v] = 0;
    ans[v] = v;
    vector<int> dd2;
    for(auto u : adj[v]){
        if(u.first == p){
            dd2.push_back(1000000009);
            continue;
        }
        calc(u.first, v);
        dp1[v] += dp1[u.first]+2;
        dd2.push_back(dp2[u.first]-dp1[u.first]);
    }
    dp2[v] = dp1[v];
    int mi = 1000000000, mindex = -1;
    for(int i = 0; i<dd2.size(); i++){
        if(dd2[i] < mi){
            mi = dd2[i];
            mindex = adj[v][i].first;
        }
    }
    if(mi == 1000000000) return;
    ans[v] = mindex;
    dp2[v] = dp2[v]+mi-1;
}

void getAns(int v, int p){
    char c;
    for(auto u : adj[v]){
        if(u.first == p) continue;
        if(u.first == ans[v]){
            c = u.second;
            continue;
        }
        cout << u.second << endl;
        getAns(u.first, v);
        if(cnt != n && 0 <= u.second-'a' && u.second-'a' <= 25) cout << '-' << endl;
    }
    if(ans[v] != v){
        cout << c << endl;
        getAns(ans[v], v);
        if(cnt != n && 0 <= c-'a' && c-'a' <= 25) cout << '-' << endl;
    }
    else cnt++;
}

int main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    nodes[0] = {0, '?'};
    for(int i = 0; i<n; i++){
        cin >> ts;
        addWord(0, -1, 0);
    }
    calc(0, -1);
    cout << dp2[0]-n+1 << endl;
    getAns(0, -1);
}

Compilation message

printer.cpp: In function 'void addWord(int, int, int)':
printer.cpp:15:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     if(index == ts.length()){
      |        ~~~~~~^~~~~~~~~~~~~~
printer.cpp: In function 'void calc(int, int)':
printer.cpp:52:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for(int i = 0; i<dd2.size(); i++){
      |                    ~^~~~~~~~~~~
printer.cpp: In function 'void getAns(int, int)':
printer.cpp:78:35: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |         if(cnt != n && 0 <= c-'a' && c-'a' <= 25) cout << '-' << endl;
      |            ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12160 KB Output is correct
2 Correct 9 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12160 KB Output is correct
2 Correct 9 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12160 KB Output is correct
2 Correct 26 ms 12288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 12792 KB Output is correct
2 Correct 49 ms 12800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 13688 KB Output is correct
2 Correct 262 ms 15608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 16120 KB Output is correct
2 Correct 102 ms 13816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 781 ms 22200 KB Output is correct
2 Execution timed out 1063 ms 33528 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 652 ms 20756 KB Output is correct
2 Execution timed out 1052 ms 37460 KB Time limit exceeded
3 Halted 0 ms 0 KB -