Submission #223070

#TimeUsernameProblemLanguageResultExecution timeMemory
223070MinnakhmetovMatch (CEOI16_match)C++14
100 / 100
20 ms11904 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5, A = 26;
int dp[N][A];
string ans, s;

void solve(int l, int r) {
    if (l > r)
        return;
    int x = dp[r][s[l] - 'a'] - 1;
    ans[l] = '(';
    ans[x] = ')';
    solve(l + 1, x - 1);
    solve(x + 1, r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> s;

    vector<char> stk;

    for (char c : s) {
        if (stk.empty() || stk.back() != c) {
            stk.push_back(c);
        }
        else {
            stk.pop_back();
        }
    }

    if (!stk.empty()) {
        cout << "-1\n";
        return 0;
    }

    int n = s.size();
    memset(dp, -1, sizeof(dp));

    for (int i = 0; i < n; i++) {
        dp[i][s[i] - 'a'] = i + 1;
        if (i > 0 && dp[i - 1][s[i] - 'a'] > 1) {
            for (int j = 0; j < A; j++) {
                dp[i][j] = max(dp[i][j], dp[dp[i - 1][s[i] - 'a'] - 2][j]);
            }
        }
    }

    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < 2; j++) {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << "\n";
    // }

    // cout << dp[4][1] << "\n";

    // return 0;

    ans.resize(n);
    solve(0, n - 1);

    cout << ans << "\n";
 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...