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;
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |