답안 #673786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673786 2022-12-22T03:23:27 Z chanhchuong123 Type Printer (IOI08_printer) C++14
20 / 100
56 ms 34916 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 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];
    }
}

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

string res;
void re_dfs(int u) {
    if (adj[u].empty()) {
        res += 'P';
        res += '-';
        return;
    }
    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); dp[0]++;
    while (dp[0]--) 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:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for (int i = 0; i < s.size(); ++i) {
      |                     ~~^~~~~~~~~~
printer.cpp: At global scope:
printer.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   59 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12084 KB Output is correct
2 Correct 6 ms 12080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 11980 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Incorrect 9 ms 11988 KB didn't print every word
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 12116 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 13012 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 15572 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 21212 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 56 ms 34916 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 29676 KB didn't print every word
2 Halted 0 ms 0 KB -