답안 #530008

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
530008 2022-02-24T11:33:03 Z Policarpo Type Printer (IOI08_printer) C++17
100 / 100
139 ms 77032 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) (5e5 + 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<int> 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(trie[u][c]);
        }
        val[trie[u][c]] = c + 'a';
        u = trie[u][c];
    }

    t[u] = 1;
}

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

    return nivel;
}

vector <char> ans;
void solve(int u) {
    if (u) ans.push_back(val[u]);
    if (t[u]) {
        ans.push_back('P');
        ++tot;
    }
    for (auto v : adj[u]) {
        if (maxn[u] == v) continue;
        solve(v);
        if (tot != n) ans.push_back('-');
    }

    if (maxn[u] != -1) {
        solve(maxn[u]);
        if (tot != n) ans.push_back('-');
    }
}

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

    memset(maxn, -1, sizeof maxn);
    a(0);
    tot = 0;
    solve(0);
    printf("%d\n", (int) ans.size());
    for (auto c : ans) {
        printf("%c\n", c);
    }
    
}

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:38: warning: array subscript has type 'char' [-Wchar-subscripts]
   39 |             adj[u].push_back(trie[u][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 'int main()':
printer.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13960 KB Output is correct
2 Correct 7 ms 13900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 13892 KB Output is correct
2 Correct 7 ms 14028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14028 KB Output is correct
2 Correct 8 ms 13964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14028 KB Output is correct
2 Correct 7 ms 13960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14092 KB Output is correct
2 Correct 8 ms 14540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14868 KB Output is correct
2 Correct 10 ms 15180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 17556 KB Output is correct
2 Correct 21 ms 21736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23064 KB Output is correct
2 Correct 17 ms 16004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 36888 KB Output is correct
2 Correct 109 ms 66936 KB Output is correct
3 Correct 60 ms 41020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 31552 KB Output is correct
2 Correct 139 ms 77032 KB Output is correct
3 Correct 71 ms 44740 KB Output is correct
4 Correct 101 ms 73412 KB Output is correct