답안 #1012432

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1012432 2024-07-02T07:13:56 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
81 ms 57304 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair <int, int>
using namespace std;
const int N = 1e6 + 10;
const int siz = 28;
struct node {
    int ch[siz];
    int vis;
    int biggest;
} trie[N];
int ncnt, rt;
char s[N][22];
vector <char> ans;
inline void insert(char *s, int ok) {
    int p = rt;
    for (int i = 0; s[i]; ++i) {
        int j = s[i] - 'a';
        if (!trie[p].ch[j])
            trie[p].ch[j] = ++ncnt;
        trie[p].biggest |= ok;
        p = trie[p].ch[j];
    }
    trie[p].biggest |= ok;
    trie[p].vis ++;
}
inline int find(char *s) {
    int p = rt;
    for (int i = 0; s[i]; ++i) {
        int j = s[i] - 'a';
        if (!trie[p].vis)
            return 0;
        p = trie[p].ch[j];
    }
    return trie[p].vis;
}
inline void dfs(int x) {
    if (trie[x].vis) {
        ans.push_back('P');
    }
    for (int i = 0; i < siz; ++i) {
        if (trie[x].ch[i] && !trie[trie[x].ch[i]].biggest) {
            ans.push_back(i + 'a');
            dfs(trie[x].ch[i]);
        }
    }
    for (int i = 0; i < siz; ++i) {
        if (trie[x].ch[i] && trie[trie[x].ch[i]].biggest) {
            ans.push_back(i + 'a');
            dfs(trie[x].ch[i]);
        }
    }
    if (x) ans.push_back('-');
}
int main() {
    int n; scanf("%d", &n);
    int maxn = 0, id = 0;
    for (int i = 1; i <= n; ++i) {
        scanf("%s", s[i]);
        if(strlen(s[i]) > maxn) {
            maxn = strlen(s[i]);
            id = i;
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (i == id) insert(s[i], 1);
        else insert(s[i], 0);
    }
    dfs(0);
    int k = ans.size() - 1;
    while (ans[k] == '-' && k > 0) k --;
    k += 1;
    printf("%d\n", k);
    for (int i = 0; i < k; ++i)
        printf("%c\n", ans[i]);
    return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:60:25: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         if(strlen(s[i]) > maxn) {
      |            ~~~~~~~~~~~~~^~~~~~
printer.cpp:56:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     int n; scanf("%d", &n);
      |            ~~~~~^~~~~~~~~~
printer.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         scanf("%s", s[i]);
      |         ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3932 KB Output is correct
2 Correct 10 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10676 KB Output is correct
2 Correct 6 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 22736 KB Output is correct
2 Correct 68 ms 48580 KB Output is correct
3 Correct 46 ms 26416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 18332 KB Output is correct
2 Correct 81 ms 57304 KB Output is correct
3 Correct 44 ms 29460 KB Output is correct
4 Correct 76 ms 54216 KB Output is correct