Submission #673792

# Submission time Handle Problem Language Result Execution time Memory
673792 2022-12-22T03:31:31 Z chanhchuong123 Type Printer (IOI08_printer) C++14
100 / 100
138 ms 76744 KB
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}

const int MAX = 500000;
int n;
string s;
int dp[MAX];
int numNode;
char c[MAX];
int cnt[MAX];
int nxt[MAX][26];
vector<int> adj[MAX];

void add(string s) {
    int p = 0;
    for (int i = 0; i < s.size(); ++i) {
        int LOVE = s[i] - 'a';
        if (nxt[p][LOVE] == 0) {
            c[numNode + 1] = s[i];
            nxt[p][LOVE] = ++numNode;
            adj[p].push_back(numNode);
        }
        p = nxt[p][LOVE];
    }
    ++cnt[p];
}

void dfs(int u) {
    for (int v: adj[u]) {
        dfs(v);
        maxi(dp[u], dp[v] + 1);
    }
}

string res;
void re_dfs(int u) {
    while (cnt[u]--) res += 'P';
    for (int v: adj[u]) {
        res += c[v];
        re_dfs(v);
    }
    res += '-';
}

main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        add(s);
    }
    dfs(0);
    for (int i = 0; i <= numNode; ++i) {
        sort(all(adj[i]), [&](int u, int v) {
            return dp[u] < dp[v];
        });
    }
    re_dfs(0);
    while (res.back() != 'P')
        res.pop_back();
    cout << res.size() << '\n';
    for (char &c: res)
        cout << c << '\n';
}

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:28:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for (int i = 0; i < s.size(); ++i) {
      |                     ~~^~~~~~~~~~
printer.cpp: At global scope:
printer.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   57 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12000 KB Output is correct
2 Correct 6 ms 12060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 7 ms 12072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12016 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 9 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 13012 KB Output is correct
2 Correct 10 ms 13268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15680 KB Output is correct
2 Correct 20 ms 20044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 21348 KB Output is correct
2 Correct 12 ms 14056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 35312 KB Output is correct
2 Correct 105 ms 66412 KB Output is correct
3 Correct 68 ms 39860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 29940 KB Output is correct
2 Correct 138 ms 76744 KB Output is correct
3 Correct 64 ms 43624 KB Output is correct
4 Correct 110 ms 73040 KB Output is correct