답안 #1117703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117703 2024-11-24T07:38:30 Z vjudge1 괄호 문자열 (CEOI16_match) C++17
10 / 100
2000 ms 504 KB
#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>;
bool cbs(string s)
{
    ll bal = 0;
    for (char i : s)
    {
        bal += i == ')' ? -1 : 1;
        if (bal < 0) return false;
    }
    return bal == 0;
}
void solve()
{
    string s;
    cin >> s;
    ll n = s.size();
    string b(n, '(');
    string ans(n, 'z');
    for (ll msk = 0; msk < (1 << n); msk++)
    {
        for (ll i = 0; i < n; i++) b[i] = (msk >> i & 1) ? ')' : '(';
        if (!cbs(b)) continue;
        bool ok = true;
        for (ll i = 0; i < n and ok; i++)
        for (ll j = i + 1; j < n and ok; j++)
            if ((j == i + 1 or cbs(b.substr(i + 1, j - i - 1))) and b[i] == '(' and b[j] == ')')
                if (s[i] != s[j]) ok = false;
        if (ok)
            ans = min(ans, b);
    }
    if (ans[0] == 'z') ans = "-1";
    cout << ans << 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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 21 ms 456 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 21 ms 456 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Execution timed out 2053 ms 336 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 21 ms 456 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Execution timed out 2053 ms 336 KB Time limit exceeded
5 Halted 0 ms 0 KB -