제출 #571862

#제출 시각아이디문제언어결과실행 시간메모리
571862FerThugGato12500괄호 문자열 (CEOI16_match)C++98
37 / 100
2073 ms660 KiB
#include<bits/stdc++.h>
using namespace std;

string s;
int n;
bool poder(int ind, string A){

    stack<int> lst;
    int D = 0;

    for(int i = 0; i < n; i++){
        if(D<0) return false;
        if(i<=ind){
            if(A[i]=='('){ lst.push(i); D++; }
            else{
                if(!lst.size() ||  s[lst.top()]!=s[i]) return false;
                lst.pop();
                D--;
            }
            continue;
        }
        if(!lst.size() || s[lst.top()]!=s[i] ){
            lst.push(i);
            D++;
        }else{
            lst.pop();
            D--;
        }
    }
    return (D==0);
}

int main(){
    cin>>s;
    n = s.size();

    string B = "";

    int j = 0;
    while(j<n){
     //       cout<<B<<" ";
        while(poder(j, B+'(')) { j++; B+='('; }
        B+=')';
        j++;
    }
    cout << (poder(n-1, B) ? B : "-1");
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...