답안 #681396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681396 2023-01-13T00:36:25 Z vjudge1 괄호 문자열 (CEOI16_match) C++14
10 / 100
2000 ms 13960 KB
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef pair<int, int> ii;

const int N = 2000 + 5;
const int mod = 1e9 + 7;
map<int, bool> f;
string s;
string t;
int n;

int encode(int a, int b)
{
    return a * 1e5 + b;
}

int dp(int l, int r)
{
    if(l > r) return 1;
    if((r - l + 1) & 1) return 0;
    
    auto it = f.insert({encode(l, r), 0});
    if(it.S == 0) return it.F -> S;
    
    bool &res = it.F -> S;
    if(s[l] == s[r]) res |= dp(l + 1, r - 1);
    for(int i = r - 1; i > l; i--)
    {
        if(s[l] == s[i]) 
            res |= (dp(l + 1, i - 1) && dp(i + 1, r));
        if(res) break;
    }
    return res;
}

void trace(int l, int r)
{
    if(l > r) return;
    
    if(s[l] == s[r])
    {
        t[l] = '(', t[r] = ')';
        trace(l + 1, r - 1);
        return;
    }
    for(int i = r - 1; i > l; i--) 
        if(s[l] == s[i] && (dp(l + 1, i - 1) && dp(i + 1, r)))
        {
            t[l] = '(', t[i] = ')';
            trace(l + 1, i - 1);
            trace(i + 1, r);
            return;
        }
}

void solve()
{
    cin >> s;
    int n = s.size();
    t.resize(n);
    if(dp(0, n - 1))
    {
        trace(0, n - 1);
        cout << t;
    }
    else cout << -1;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 191 ms 2884 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Execution timed out 2094 ms 13960 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 191 ms 2884 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Execution timed out 2094 ms 13960 KB Time limit exceeded
8 Halted 0 ms 0 KB -