#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
4 ms |
308 KB |
Output is correct |
7 |
Correct |
7 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
4 ms |
308 KB |
Output is correct |
7 |
Correct |
7 ms |
212 KB |
Output is correct |
8 |
Runtime error |
46 ms |
440 KB |
Execution killed with signal 6 |
9 |
Halted |
0 ms |
0 KB |
- |