답안 #780168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
780168 2023-07-12T07:05:52 Z raysh07 Type Printer (IOI08_printer) C++17
100 / 100
180 ms 113336 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
 
mt19937 rng;
 
typedef struct Node {
    int adj[26], par; bool end;
    Node(int p = -1) {
        end = 0; par = p;
        fill(adj, adj + 26, -1);
    }
} node;
 
vector<node> tree(1);
 
int add(string &s) {
    int cur = 0;
    for (char c : s) {
        if (tree[cur].adj[c - 'a'] == -1) {
            tree[cur].adj[c - 'a'] = sz(tree);
            tree.emplace_back(cur);
        }
        cur = tree[cur].adj[c - 'a'];
    }
    tree[cur].end = true;
    return cur;
}
 
int cnt = 0, N;
vector<string> res;
vector<int> mx;
 
void dfs(int u) {
    if (tree[u].end) {
        res.pb("P");
        cnt++;
    }
    vector<vector<int>> srt;
    for (int i = 0; i < 26; i++) {
        if (tree[u].adj[i] != -1) {
            srt.pb({mx[tree[u].adj[i]], i});
            
        }
    }
    sort(all(srt));
    for (vector<int> v : srt) {
        int i = v[1];
        string s = ""; s += (char)(i + 'a');
        res.pb(s);
        dfs(tree[u].adj[i]);
        if (cnt < N) res.pb("-");
    }
}
 
void solve() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        string s; cin >> s;
        add(s);
    }
    mx = vector<int>(sz(tree));
    fill(all(mx), 0);
    for (int i = sz(tree) - 1; i > 0; i--) {
        mx[tree[i].par] = max(mx[tree[i].par], mx[i] + 1);
    }
    dfs(0);
    cout << sz(res) << "\n";
    for (string s : res) {
        cout << s << "\n";
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
    
    solve();
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1996 KB Output is correct
2 Correct 4 ms 2888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 6980 KB Output is correct
2 Correct 22 ms 14352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 19904 KB Output is correct
2 Correct 8 ms 4936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 46300 KB Output is correct
2 Correct 145 ms 100592 KB Output is correct
3 Correct 73 ms 51672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 39192 KB Output is correct
2 Correct 180 ms 113336 KB Output is correct
3 Correct 79 ms 56476 KB Output is correct
4 Correct 144 ms 108896 KB Output is correct