Submission #448041

# Submission time Handle Problem Language Result Execution time Memory
448041 2021-07-28T16:14:37 Z Alex_tz307 Type Printer (IOI08_printer) C++17
100 / 100
163 ms 99520 KB
#include <bits/stdc++.h>

using namespace std;

struct node {
  int level;
  bool is_end, is_marked;
  node *son[26];

  node() {
    level = is_end = is_marked = 0;
    for (int i = 0; i < 26; ++i)
      son[i] = nullptr;
  }
};

string s;
int longest;
bool found;
vector<char> sol;

void max_self(int &x, int y) {
  x = max(x, y);
}

void Insert(node *nod, int p) {
  max_self(longest, nod->level);
  if (p == (int)s.size()) {
    nod->is_end = true;
    return;
  }
  int nxt = s[p] - 'a';
  if (nod->son[nxt] == nullptr) {
    nod->son[nxt] = new node;
    nod->son[nxt]->level = nod->level + 1;
  }
  Insert(nod->son[nxt], p + 1);
}

bool mark(node *nod) {
  bool is_leaf = true;
  for (int i = 0; i < 26 && is_leaf; ++i)
    if (nod->son[i] != nullptr)
      is_leaf = false;
  if (is_leaf) {
    if (found)
      return false;
    if (nod->level == longest) {
      nod->is_marked = true;
      found = true;
      return true;
    }
    return false;
  }
  bool ok = false;
  for (int i = 0; i < 26 && !ok; ++i)
    if (nod->son[i] != nullptr)
      ok |= mark(nod->son[i]);
  nod->is_marked = ok;
  return ok;
}

void solve(node *nod) {
  if (nod->is_end)
    sol.emplace_back('P');
  int marked = -1;
  for (int i = 0; i < 26; ++i) {
    if (nod->son[i] != nullptr) {
      if (nod->son[i]->is_marked == false) {
        sol.emplace_back('a' + i);
        solve(nod->son[i]);
        sol.emplace_back('-');
      } else marked = i;
    }
  }
  if (marked == -1)
    return;
  sol.emplace_back('a' + marked);
  solve(nod->son[marked]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  node* root = new node;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    Insert(root, 0);
  }
  mark(root);
  solve(root);
  cout << sol.size() << '\n';
  for (auto it : sol)
    cout << it << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1740 KB Output is correct
2 Correct 4 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5864 KB Output is correct
2 Correct 21 ms 12620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 14668 KB Output is correct
2 Correct 8 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 36368 KB Output is correct
2 Correct 125 ms 83636 KB Output is correct
3 Correct 78 ms 43196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 28480 KB Output is correct
2 Correct 151 ms 99520 KB Output is correct
3 Correct 81 ms 48960 KB Output is correct
4 Correct 163 ms 94012 KB Output is correct