답안 #529889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529889 2022-02-24T00:03:29 Z Policarpo Type Printer (IOI08_printer) C++17
60 / 100
1000 ms 32588 KB
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <set>
#include <stack>
#include <bitset>
#define ll long long
#define INF (1e9)
#define MAX (int) (1e5 + 5)
#define MOD 1000000007
#define par pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(x) (int) ((x).size())
#define esq(x) (x<<1)
#define dir(x) ((x<<1)|1)
#define lsb(x) (x & -x)
#define W(x) cout << #x << ": " << x << endl
#define Wii(x) cout << x.first << ' ' << x.second << endl

using namespace std;

int n, t[MAX], trie[MAX][26], qnt, tot, resp, maxn[MAX];
char val[MAX];
char s[22];
vector<pair<int, char>> adj[MAX];

void add(char s[]) {
    int u = 0, ts = strlen(s);
    for (int i = 0; i < ts; i++) {
        char c = s[i] - 'a';
        if (!trie[u][c]) {
            trie[u][c] = ++qnt;
            adj[u].push_back(make_pair(trie[u][c], c));
        }
        val[trie[u][c]] = c+'a';
        u = trie[u][c];
    }

    t[u] = 1;
}

int a(int u) {
    int nivel = 0;
    for (auto x : adj[u]) {
        int v = x.first;
        if (nivel < 1 + a(v)) {
            nivel = 1 + a(v);
            maxn[u] = v;
        }
    }

    return nivel;
}

void b(int u) {
    ++resp;
    if (t[u]) {
        ++tot;
        ++resp;
    }
    for (auto x : adj[u]) {
        int v = x.first;
        if (maxn[u] == v) continue;
        b(v);
        if (tot != n) {
            ++resp;
        }
    }

    if (maxn[u]!=-1) {
        b(maxn[u]);
        if (tot != n) resp++;
    }
}

void solve(int u) {
    if (u) printf("%c\n", val[u]);
    if (t[u]) {
        printf("P\n");
        ++tot;
    }
    for (auto x : adj[u]) {
        int v = x.first;
        char c = x.second;
        if (maxn[u] == v) continue;
        solve(v);
        if (tot != n) printf("-\n");
    }

    if (maxn[u]!=-1) {
        solve(maxn[u]);
        if (tot != n) printf("-\n");
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", s);
        add(s);
    }

    memset(maxn, -1, sizeof maxn);
    a(0);
    b(0);
    cout << --resp << endl;
    tot = 0;
    solve(0);
}

Compilation message

printer.cpp: In function 'void add(char*)':
printer.cpp:37:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   37 |         if (!trie[u][c]) {
      |                      ^
printer.cpp:38:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |             trie[u][c] = ++qnt;
      |                     ^
printer.cpp:39:48: warning: array subscript has type 'char' [-Wchar-subscripts]
   39 |             adj[u].push_back(make_pair(trie[u][c], c));
      |                                                ^
printer.cpp:41:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   41 |         val[trie[u][c]] = c+'a';
      |                     ^
printer.cpp:42:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   42 |         u = trie[u][c];
      |                     ^
printer.cpp: In function 'void solve(int)':
printer.cpp:90:14: warning: unused variable 'c' [-Wunused-variable]
   90 |         char c = x.second;
      |              ^
printer.cpp: In function 'int main()':
printer.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3060 KB Output is correct
2 Correct 2 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3020 KB Output is correct
2 Correct 2 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3020 KB Output is correct
2 Correct 4 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 179 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 3932 KB Output is correct
2 Correct 110 ms 4188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 6392 KB Output is correct
2 Execution timed out 1091 ms 10188 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 11840 KB Output is correct
2 Execution timed out 1096 ms 4684 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 26 ms 32588 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 27 ms 32324 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -