Submission #1293961

#TimeUsernameProblemLanguageResultExecution timeMemory
1293961darkdevilvaqifMatch (CEOI16_match)C++20
0 / 100
0 ms332 KiB
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define all(v, l) v.begin() + l, v.end()
#define rall(v, l) v.rbegin(), v.rend() - l
#define pb push_back
#define pf push_front
#define rsz resize
#define fi first
#define se second
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
#define IMAX INT_MAX
#define IMIN INT_MIN
#define endl "\n"
#define newline cout << endl;
using namespace std;
 
// structs

// globals

// variables
int n;
string s, ans;

// iterators
int i, j;

// notes
/*
 -stuff you should look for-
* int overflow, array bounds
* special cases (n=1?)
* do something instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH

continue - skip the rest in the loop
*/

// functions
ll GCD(ll numeroune, ll numerodeux);
ll LCM(ll numeroune, ll numerodeux);
ll power(ll numeroune, ll numerodeux);
 
void solve()
{
    cin >> s;
    n = s.size();
    
    ans = "";
    stack <char> st;
    for (i = 0; i < n; i++)
    {
        if (!st.empty() && st.top() == s[i])
        {
            st.pop();
        }
        else
        {
            st.push(s[i]);
        }
    }
    
    if (!st.empty())
    {
        cout << -1;
        return;
    }
    
    while (!st.empty())
    {
        st.pop();
    }
    for (i = 0; i < n; i++)
    {
        auto temp = st;
        temp.push(s[i]);
        
        for (j = i + 1; j < n; j++)
        {
            if (!temp.empty() && temp.top() == s[j])
            {
                temp.pop();
            }
            else
            {
                temp.push(s[j]);
            }
        }
        
        if (!temp.empty())
        {
            ans += ')';
            st.pop();
        }
        else
        {
            st.push(s[i]);
        }
    }
    
    cout << ans;
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        newline
    }
}
 
ll GCD(ll numeroune, ll numerodeux)
{
    if (!numeroune)
    {
        return numerodeux;
    }
    return GCD(numerodeux % numeroune, numeroune);
}

ll LCM(ll numeroune, ll numerodeux)
{
    return numeroune * numerodeux / GCD(numeroune, numerodeux);
}

ll power(ll numeroune, ll numerodeux)
{
    ll res = 1;
    while (numerodeux) 
    {
        if (numerodeux & 1)
        { 
            res *= numeroune;
        }
        numeroune *= numeroune;
        numerodeux >>= 1;    
    }
    return res;
}

/*
$$$$$$$$\ $$$$$$$$\ 
$$  _____|\____$$  |
$$ |          $$  / 
$$$$$\       $$  /  
$$  __|     $$  /   
$$ |       $$  /    
$$$$$$$$\ $$$$$$$$\ 
\________|\________|
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...