Submission #788618

# Submission time Handle Problem Language Result Execution time Memory
788618 2023-07-20T12:18:01 Z WLZ Match (CEOI16_match) C++17
100 / 100
62 ms 36756 KB
#include <bits/stdc++.h>
using namespace std;

class trie {
  private:
    struct trie_node {
      trie_node *parent;
      vector<trie_node*> children;
      vector<int> val;
    } *cur;
    string s;
  public:
    trie() {
      cur = new trie_node{nullptr, vector<trie_node*>(26, nullptr), vector<int>(26, -1)};
      s = "";
    }

    void update(char c, int x) {
      cur->val[c - 'a'] = x;
    }

    int query(char c) {
      return cur->val[c - 'a'];
    }

    void go_up() {
      cur = cur->parent;
      s.pop_back();
    }

    void go_down(char c) {
      if (cur->children[c - 'a'] == nullptr) cur->children[c - 'a'] = new trie_node{cur, vector<trie_node*>(26, nullptr), vector<int>(26, -1)};
      cur = cur->children[c - 'a'];
      s.push_back(c);
    }

    const char current_char() {
      if (cur->parent == nullptr) return '$';
      return s.back();
    }
};

int n;
string s;
vector< vector<int> > prev_l;

string solve(int l, int r) {
  if (l > r) return "";
  if (l + 1 == r) return "()";
  return "(" + solve(l + 1, prev_l[r][s[l] - 'a'] - 1) + ")" + solve(prev_l[r][s[l] - 'a'] + 1, r);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  int n = (int) s.length();
  trie mp;
  prev_l.assign(n, vector<int>(26, -1));
  for (int i = 0; i < n; i++) {
    if (mp.current_char() == s[i]) mp.go_up();
    else mp.go_down(s[i]);
    mp.update(s[i], i);
    for (int j = 0; j < 26; j++) prev_l[i][j] = mp.query(j + 'a');
  }
  if (mp.current_char() != '$') cout << -1 << '\n';
  else cout << solve(0, n - 1) << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 2 ms 1992 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2896 KB Output is correct
11 Correct 2 ms 3156 KB Output is correct
12 Correct 31 ms 23288 KB Output is correct
13 Correct 30 ms 25372 KB Output is correct
14 Correct 33 ms 28460 KB Output is correct
15 Correct 51 ms 18508 KB Output is correct
16 Correct 40 ms 18480 KB Output is correct
17 Correct 39 ms 27400 KB Output is correct
18 Correct 30 ms 15688 KB Output is correct
19 Correct 42 ms 34780 KB Output is correct
20 Correct 27 ms 24652 KB Output is correct
21 Correct 62 ms 36756 KB Output is correct