Submission #341614

# Submission time Handle Problem Language Result Execution time Memory
341614 2020-12-30T08:55:50 Z phathnv Match (CEOI16_match) C++11
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2001;
const int AL = 26;

int n, dp[N][N], match[N];
string s;

void readInput(){
    cin >> s;
    n = s.size();
    s = '*' + s;
}

void calcDp(int l){
    stack <int> st;
    dp[l][l - 1] = 1;
    for(int i = l; i <= n; i++){
        if (st.empty())
            st.push(i);
        else if (s[st.top()] == s[i])
            st.pop();
        else
            st.push(i);

        if (st.empty())
            dp[l][i] = 1;
    }
}

void process(int l, int r){
    if (l > r)
        return;

    for(int i = r; i >= l; i--)
        if (s[l] == s[i] && dp[l + 1][i - 1]){
            match[l] = i;
            match[i] = l;
            process(l + 1, i - 1);
            process(i + 1, r);
        }
}

void solve(){
    for(int i = 1; i <= n; i++)
        calcDp(i);
    if (!dp[1][n]){
        cout << -1;
        return;
    }
    process(1, n);
    for(int i = 1; i <= n; i++)
        cout << (match[i] > i? '(' : ')');
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    readInput();
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -