제출 #616108

#제출 시각아이디문제언어결과실행 시간메모리
616108elpro123괄호 문자열 (CEOI16_match)C++14
37 / 100
46 ms440 KiB
#include <bits/stdc++.h>
using namespace std;
 
const int MAXN = 2004;
 
int N;
string s;
char ans[MAXN];
 
bool check(stack<char> st, int x){
    //mira si entra en el caso de ser una cadena vacia
	for(int i = x; i<N; i++){
        //cout<<s[i]<<" "<<st.top()<<"\n";
		if(!st.empty() && s[i] == st.top()){//hay un par de iguales ()
            st.pop();
        }else{//no hay un par, hay otro elemento distinto
            st.push(s[i]);
        }
        //cout<<st.empty()<<"\n";
    }
	return st.empty();
}
 
int main() {
	cin>>s;
    N=s.length();
	stack<char> st;
	for(int i = 0; i < N; i++) {
		st.push(s[i]);
		if(check(st, i + 1)){//se habre con (
            ans[i] = '(';
        }else{//hay otro distinto
			ans[i] = ')';
			st.pop();
            //si nos quedamos sin elementos y no se completaron los pares, -1
            //o no se puede formar un par con los elementos sobrantes, -1
			if(st.empty() || st.top() != s[i]){
				cout<<"-1\n";
				return 0;
			}
			st.pop();//procedemos a verificar otro par
		}
        //cout<<st.top()<<"\n";
	}
    
	if(st.empty()){ 
        for(int i = 0; i < N; i++){
           cout << ans[i];
        }
    }else{ 
        cout<<"-1\n";
    }

	return 0;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...