This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |