Submission #153419

#TimeUsernameProblemLanguageResultExecution timeMemory
153419lycMatch (CEOI16_match)C++14
37 / 100
2053 ms528 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x) 
#define REP(i, n) for (int i = 0; i < n; ++i) 
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    string S; cin >> S;
    int N = S.length();
    string T = "";
    stack<char> stk;
    FOR(i,0,N-1) if (stk.empty() || stk.top() != S[i]) stk.push(S[i]); else stk.pop();
    if (!stk.empty()) { cout << -1 << '\n'; }
    else {
        FOR(i,0,N-1) {
            int M = (int)T.length();
            FOR(j,0,M-1) if (T[j] == '(') stk.push(S[j]); else stk.pop();

            stk.push(S[i]);
            //cout << i << " i stk " << stk.size() << " :: " << T << endl;
            FOR(j,M+1,N-1) if (stk.empty() || stk.top() != S[j]) stk.push(S[j]); else stk.pop();
            T += (stk.empty() ? '(' : ')');

            while (!stk.empty()) stk.pop();
        }
        cout << T << '\n';
    }
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...