#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#pragma GCC optimize ("03,unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
int n;
string str;
string res;
bool is_possible(int i,vector<char>p){
for(int j=i;j<n;j++){
if(p.empty()||p.back()!=str[j]){
p.push_back(str[j]);
}else{
p.pop_back();
}
}
return p.empty();
}
void solve(int l,int r){
if(l>=r)
return;
for(int i=r-1;i>=l;i--){
if(str[i]==str[l]){
res[i]=')';
res[l]='(';
solve(l+1,i);
solve(i+1,r);
return;
}
}
}
void solve(){
cin>>str;
n=(int)str.size();
if(!is_possible(0,{})){
cout<<"-1\n";
return;
}
res=string(n,'-');
solve(0,n);
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("input.in","r",stdin);freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
abbaaa
(()())
abab
-1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |