#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;
void solve1() {
str s;
cin>>s;
str seq;
vector<pair<char,char>> st;
ll n = s.size();
map<char,ll> open_e;
map<char, ll> open_o;
map<ll,ll> cnt;
map <char,ll> cnt_even;
map <char, ll> cnt_odd;
for (int i = 0; i < n; i++){
if (i%2==0){
cnt_even[s[i]]++;
}
else cnt_odd[s[i]]++;
}
for (int i = 0; i < n; i++){
if (i%2) cnt_odd[s[i]]--;
else cnt_even[s[i]]--;
bool f = true;
if (i%2) open_o[s[i]]++;
else open_e[s[i]]++;
for (auto &u : open_e) {
if (cnt_odd[u.first]< u.second) {
f = false;
break;
}
}
for(auto &u : open_o){
if (cnt_even[u.first]<u.second){
f= false;
break;
}
}
if (i%2) open_o[s[i]]--;
else open_e[s[i]]--;
if (f){
st.push_back({'(',s[i]});
seq.push_back('(');
if (i%2) open_o[s[i]]++;
else open_e[s[i]]++;
}
else {
if (st.empty()|| st.back()!=make_pair('(',s[i])){
cout<<-1<<endl;
return;
}
st.pop_back();
seq.push_back(')');
if (i%2) open_e[s[i]]--;
else open_o[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... |