답안 #52882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52882 2018-06-27T06:13:42 Z 노영훈(#1382) 괄호 문자열 (CEOI16_match) C++11
10 / 100
1678 ms 3020 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=500010, inf=2e9;

int n;

char S[MX], R[MX];

bool solve(int s){
    for(int i=0; i<n; i++)
        R[i+1] = (s&(1<<(n-i-1)))!=0 ? ')' : '(';
    int sum=0;
    for(int i=1; i<=n; i++) sum+=(R[i]=='(' ? 1 : -1);
    if(sum!=0) return false;
    int mat[MX]={};
    bool done[MX]={};
    for(int i=1; i<=n; i++){
        if(done[i]) continue;
        for(int j=i, cnt=0; j<=n; j++){
            cnt+= ( R[j]=='(' ? 1 : -1 );
            if(cnt<0) return false;
            if(cnt==0){
                mat[i]=j;
                done[i]=done[j]=true;
                break;
            }
        }
        if(S[i]!=S[mat[i]]) return false;
    }
    return true;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>(S+1);
    for(int i=1; S[i]!=0; i++) n=i;
    if(n>20){
        cout<<-1;
        return 0;
    }
    for(int i=0; i<(1<<n); i++){
        if(solve(i)){
            cout<<(R+1);
            return 0;
        }
    }
    cout<<-1;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2812 KB Output is correct
3 Correct 1678 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2812 KB Output is correct
3 Correct 1678 ms 3020 KB Output is correct
4 Incorrect 2 ms 3020 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2812 KB Output is correct
3 Correct 1678 ms 3020 KB Output is correct
4 Incorrect 2 ms 3020 KB Output isn't correct
5 Halted 0 ms 0 KB -