답안 #689939

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
689939 2023-01-29T19:28:30 Z gagik_2007 Type Printer (IOI08_printer) C++17
90 / 100
1000 ms 58412 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

const int K = 26;

class Vertex {
public:
    int size;
    bool word;
    int to[K];
    Vertex() {
        fill(to, to + K, -1);
        size = 0;
        word = false;
    }
};

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 1000007;
ll n, m, k;
vector<Vertex>p(1);
vector<char>ans;

void add(const string& s) {
    int v = 0;
    for (auto ch : s) {
        int c = ch - 'a';
        if (p[v].to[c] == -1) {
            p[v].to[c] = p.size();
            p.emplace_back();
        }
        v = p[v].to[c];
    }
    p[v].word = true;
}

void calc_size(int v) {
    p[v].size = 1;
    for (int i = 0; i < K; i++) {
        if (p[v].to[i] != -1) {
            calc_size(p[v].to[i]);
            p[v].size = max(p[v].size, p[p[v].to[i]].size + 1);
        }
    }
}

void dfs(int v) {
    if (p[v].word) {
        ans.push_back('P');
    }
    vector<pair<int, pair<int,int>>>d;
    for (int i = 0; i < K; i++) {
        if (p[v].to[i] != -1) {
            d.push_back({ p[p[v].to[i]].size,{p[v].to[i],i} });
        }
    }
    sort(d.begin(), d.end());
    for (int i = 0; i < d.size(); i++) {
        ans.push_back(d[i].ss.ss + 'a');
        dfs(d[i].ss.ff);
        ans.push_back('-');
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        add(s);
    }
    calc_size(0);
    dfs(0);
    while ((*ans.rbegin()) == '-') {
        ans.pop_back();
    }
    cout << ans.size() << endl;
    for (auto x : ans) {
        cout << x << endl;
    }
    return 0;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -

Compilation message

printer.cpp: In function 'void dfs(int)':
printer.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for (int i = 0; i < d.size(); i++) {
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 10 ms 884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 1296 KB Output is correct
2 Correct 22 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 4040 KB Output is correct
2 Correct 143 ms 7620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 7908 KB Output is correct
2 Correct 47 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 29220 KB Output is correct
2 Correct 912 ms 57848 KB Output is correct
3 Correct 504 ms 29156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 349 ms 15388 KB Output is correct
2 Execution timed out 1088 ms 58412 KB Time limit exceeded
3 Halted 0 ms 0 KB -