Submission #567366

# Submission time Handle Problem Language Result Execution time Memory
567366 2022-05-23T11:02:06 Z piOOE Match (CEOI16_match) C++17
100 / 100
20 ms 13012 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) begin(x), end(x)
#define sz(x) ((int)size(x))
#define trace(x) cout << #x << ": " << (x) << endl;

typedef long long ll;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int A = 26, N = 100000;

int mx_prv[N][A];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s, ans;
    cin >> s;
    int n = sz(s);
    ans.resize(n);
    memset(mx_prv, -1, sizeof(mx_prv));
    for (int i = 1; i < n; ++i) {
        mx_prv[i][s[i] - 'a'] = i;
        for (int c = 0; c < A; ++c) {
            int j = mx_prv[i - 1][s[i] - 'a'] - 1;
            if (j >= 0 && mx_prv[i][c] == -1) {
                if ((s[j] - 'a') == c) {
                    mx_prv[i][c] = j;
                } else {
                    mx_prv[i][c] = mx_prv[j][c];
                }
            }
        }
    }
    vector<int> st;
    for (int i = 0; i < n; ++i) {
        if (st.empty() || s[st.back()] != s[i]) {
            st.push_back(i);
        } else {
            st.pop_back();
        }
    }
    if (!st.empty()) {
        cout << -1;
        return 0;
    }
    function<void(int, int)> solve = [&](int l, int r) {
        if (l >= r) return;
        int m = mx_prv[r][s[l] - 'a'];
        ans[l] = '(', ans[m] = ')';
        solve(l + 1, m - 1), solve(m + 1, r);
    };
    solve(0, n - 1);
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 4 ms 10436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 4 ms 10436 KB Output is correct
4 Correct 5 ms 10452 KB Output is correct
5 Correct 5 ms 10452 KB Output is correct
6 Correct 5 ms 10452 KB Output is correct
7 Correct 5 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 4 ms 10436 KB Output is correct
4 Correct 5 ms 10452 KB Output is correct
5 Correct 5 ms 10452 KB Output is correct
6 Correct 5 ms 10452 KB Output is correct
7 Correct 5 ms 10452 KB Output is correct
8 Correct 6 ms 10452 KB Output is correct
9 Correct 6 ms 10580 KB Output is correct
10 Correct 5 ms 10540 KB Output is correct
11 Correct 5 ms 10628 KB Output is correct
12 Correct 11 ms 11860 KB Output is correct
13 Correct 14 ms 11988 KB Output is correct
14 Correct 14 ms 12636 KB Output is correct
15 Correct 15 ms 12716 KB Output is correct
16 Correct 20 ms 12756 KB Output is correct
17 Correct 14 ms 12968 KB Output is correct
18 Correct 14 ms 11600 KB Output is correct
19 Correct 14 ms 12476 KB Output is correct
20 Correct 10 ms 12276 KB Output is correct
21 Correct 15 ms 13012 KB Output is correct