답안 #868630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868630 2023-11-01T03:32:33 Z LOLOLO Type Printer (IOI08_printer) C++17
100 / 100
107 ms 106504 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 5e5;

struct node {
    char c;
    int v = 0, end = 0;
    node * child[26];
    node() {
        for (int i = 0; i < 26; i++) {
            child[i] = NULL;
        }
    }
};

string ans;
node *root = new node;

void insert(string s) {
    node *cur = root;

    for (auto x : s) {
        int ind = x - 'a';
        if (cur -> child[ind] == NULL) {
            cur -> child[ind] = new node;
            cur -> child[ind] -> c = x;
        }

        cur = cur -> child[ind];
    }

    cur -> end++;
}

void reverse(string s) {
    node *cur = root;
    for (auto x : s) {
        int ind = x - 'a';
        swap(cur -> child[25], cur -> child[ind]);
        cur = cur -> child[25];
    }
}

void dfs(node *cur) {
    for (int i = 0; i < cur -> end; i++)
        ans += "P";

    for (int i = 0; i < 26; i++) {
        if (cur -> child[i] == NULL)
            continue;

        ans += (cur -> child[i] -> c);
        dfs(cur -> child[i]);
        ans += "-";
    }
}

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

    int n;
    cin >> n;
    string s, mx;
    for (int i = 0; i < n; i++) {
        cin >> s;
        if (len(s) > len(mx)) {
            mx = s;
        }

        insert(s);
    }


    reverse(mx);
    dfs(root);
    while (ans.back() == '-')
        ans.pop_back();

    cout << len(ans) << '\n';
    for (auto x : ans)
        cout << x << '\n';

    return 0;
}
# 결과 실행 시간 메모리 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 348 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 0 ms 348 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 1 ms 348 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6492 KB Output is correct
2 Correct 13 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15808 KB Output is correct
2 Correct 6 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 39148 KB Output is correct
2 Correct 88 ms 89612 KB Output is correct
3 Correct 48 ms 46252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 30704 KB Output is correct
2 Correct 107 ms 106504 KB Output is correct
3 Correct 53 ms 52464 KB Output is correct
4 Correct 91 ms 100504 KB Output is correct