답안 #1084619

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084619 2024-09-06T14:20:21 Z Hamed5001 Type Printer (IOI08_printer) C++14
100 / 100
140 ms 58752 KB
#include <bits/stdc++.h>

using namespace std;

const int K = 26;
struct Node {
  int next[K];
  int mx_depth = 0;
  bool output = false;

  Node() {
    fill(next, next+K, -1);
  }
};

vector<Node> trie(1);

void add_string(string const& s) {
  int v = 0;
  stack<int> st;
  for (char ch : s) {
    st.push(v);
    int c = ch - 'a';
    if (trie[v].next[c] == -1) {
      trie[v].next[c] = trie.size();
      trie.emplace_back();
    }
    v = trie[v].next[c];
  }
  trie[v].output = true;
  while (!st.empty()) {
    v = st.top();
    st.pop();
    for (int i = 0; i < K; ++i) {
      if (~trie[v].next[i])
        trie[v].mx_depth = max(trie[v].mx_depth, trie[trie[v].next[i]].mx_depth+1);
    }
  }
}

vector<char> ans;
void dfs(int v = 0) {
  if (trie[v].output) ans.push_back('P');
  set<array<int, 3>> st; 
  for (int i = 0; i < K; ++i) {
    if (~trie[v].next[i]) st.insert({trie[trie[v].next[i]].mx_depth, trie[v].next[i], i});
  }
  while (!st.empty()) {
    auto it = *st.begin();
    st.erase(st.begin());
    ans.push_back('a' + it[2]);
    dfs(it[1]);
    ans.push_back('-');
  }
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int N; cin >> N;
    for (int i = 0; i < N; ++i) {
      string s; cin >> s;
      add_string(s);
    }
    dfs(0);
    while (!ans.empty() && ans.back() == '-') ans.pop_back();
    cout << ans.size() << endl;
    for (auto it : ans) cout << it << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 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 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1364 KB Output is correct
2 Correct 3 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4052 KB Output is correct
2 Correct 16 ms 7628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8164 KB Output is correct
2 Correct 12 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 29388 KB Output is correct
2 Correct 117 ms 58236 KB Output is correct
3 Correct 64 ms 29744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 15556 KB Output is correct
2 Correct 140 ms 58496 KB Output is correct
3 Correct 72 ms 29728 KB Output is correct
4 Correct 117 ms 58752 KB Output is correct