Submission #1127687

#TimeUsernameProblemLanguageResultExecution timeMemory
1127687NeltMatch (CEOI16_match)C++20
100 / 100
48 ms28228 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define endl "\n"

using namespace std;
using namespace __gnu_pbds;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T, typename key = less_equal<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 1e5 + 5;
string s;
ll n, dp[N][26];
string f(ll l, ll r)
{
    if (l > r)
        return "";
    return "(" + f(l + 1, dp[r][s[l] - 'a'] - 1) + ")" + f(dp[r][s[l] - 'a'] + 1, r);
}
void solve()
{
    cin >> s;
    n = s.size();
    s = ' ' + s;
    stack<ll> st;
    for (ll i = 1; i <= n; i++)
        if (!st.empty() and st.top() == s[i])
            st.pop();
        else
            st.push(s[i]);
    if (!st.empty())
    {
        cout << "-1\n";
        return;
    }
    for (ll i = 2; i <= n; i++)
    for (ll j = 0; j < 26; j++) if (j + 'a' == s[i]) dp[i][j] = i;
    else dp[i][j] = dp[i - 1][s[i] - 'a'] - 1 < 0 ? 0 : dp[dp[i - 1][s[i] - 'a'] - 1][j];
    cout << f(1, n) << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    // cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...