#ifndef local
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll,ll>;
using str = string;
using ld = long double;
using hash_map =gp_hash_table<int, int>;
using hash_set= gp_hash_table <int, null_type>;
auto sd = std::chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(sd);
typedef tree<ll, null_type, less<>, rb_tree_tag,
tree_order_statistics_node_update> ord_set;
const ll maxn = 3e5+8;
const ll inf = 2e18+7;
const ll logn = 16;
const ll mod = 1e9+7;
bool exist(str s, ll id, vector<pair<char,char>>st) {
for (int j = id; j < (ll)s.size(); j++) {
if (!st.empty() && s[j]==st.back().second) {
st.pop_back();
}
else {
st.push_back({'(', s[j]});
}
}
return (st.empty());
}
void solve1() {
str s;
cin>>s;
str seq;
vector<pair<char,char>> st;
ll n = s.size();
for (int i = 0; i < n; i++){
st.push_back({'(',s[i]});
bool exists = exist(s, i+1, st);
st.pop_back();
if (!exists){
if (st.empty()|| st.back()!=make_pair('(',s[i])){
cout<<-1<<endl;
return;
}
st.pop_back();
seq.push_back(')');
}
else {
seq.push_back('(');
st.push_back({'(',s[i]});
}
}
if (!st.empty()){
cout<<-1<<endl;
return;
}
cout<<seq;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
solve1();
#ifdef local
printf_s("\n%.5f s", (double) clock() / CLOCKS_PER_SEC);
#endif
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |