Submission #1084624

# Submission time Handle Problem Language Result Execution time Memory
1084624 2024-09-06T14:38:53 Z Hamed5001 Type Printer (IOI08_printer) C++14
100 / 100
108 ms 51664 KB
#include <bits/stdc++.h>

using namespace std;

const int K = 26, mxN = 1e6;
int trie[mxN][K], cnt[mxN], last;
int d[mxN];

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][c])
      trie[v][c] = ++last; 
    v = trie[v][c];
  }
  cnt[v]++;
  while (!st.empty()) {
    v = st.top();
    st.pop();
    for (int i = 0; i < K; ++i)
      if (trie[v][i])
        d[v] = max(d[v], d[trie[v][i]]+1);
  }
}

vector<char> ans;
void dfs(int v = 0) {
  if (cnt[v]) ans.push_back('P');
  set<array<int, 3>> st; 
  for (int i = 0; i < K; ++i) {
    if (trie[v][i]) st.insert({d[trie[v][i]], trie[v][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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1116 KB Output is correct
2 Correct 3 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3416 KB Output is correct
2 Correct 13 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 7948 KB Output is correct
2 Correct 11 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 19308 KB Output is correct
2 Correct 87 ms 43584 KB Output is correct
3 Correct 55 ms 22740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 15236 KB Output is correct
2 Correct 108 ms 51664 KB Output is correct
3 Correct 59 ms 25744 KB Output is correct
4 Correct 89 ms 48740 KB Output is correct