Submission #689940

# Submission time Handle Problem Language Result Execution time Memory
689940 2023-01-29T19:30:48 Z gagik_2007 Type Printer (IOI08_printer) C++17
100 / 100
167 ms 58304 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);
    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();
    }
    printf("%d\n", ans.size());
    for (auto x : ans) {
        printf("%c\n", x);
    }
    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++) {
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:106:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wformat=]
  106 |     printf("%d\n", ans.size());
      |             ~^     ~~~~~~~~~~
      |              |             |
      |              int           std::vector<char>::size_type {aka long unsigned int}
      |             %ld
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1296 KB Output is correct
2 Correct 4 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4040 KB Output is correct
2 Correct 24 ms 7620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7804 KB Output is correct
2 Correct 13 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 29156 KB Output is correct
2 Correct 139 ms 57976 KB Output is correct
3 Correct 72 ms 29152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 15176 KB Output is correct
2 Correct 167 ms 57808 KB Output is correct
3 Correct 89 ms 29616 KB Output is correct
4 Correct 148 ms 58304 KB Output is correct