#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
bool check_validity(string s){
string st;
for(char c: s){
if (st.empty() || st.back() != c) st.push_back(c);
else st.pop_back();
}
return st.empty();
}
void solve(){
string s; cin >> s;
if (!check_validity(s)){
cout << "-1\n";
return;
}
int n = s.size();
s = string(1, 'z' + 1) + s + string(2, 'z' + 2);
int nxt[n + 3][28];
int jump[17][n + 3];
memset(nxt, -1, sizeof nxt);
memset(jump, 0, sizeof jump);
for(int i = 2; i <= n + 2; ++i){
if (s[i-1] == s[i]) {
int digit = s[i-2] - 'a';
jump[0][i] = i - 2;
nxt[i][digit] = i-2;
nxt[i][s[i] - 'a'] = i;
for(int j = 0; j < 28; ++j)
maximize(nxt[i][j], nxt[i - 2][j]);
}
else{
int idx = nxt[i-1][s[i] - 'a'];
if (idx == -1) continue;
int digit = s[idx-1] - 'a';
jump[0][i] = idx - 1;
nxt[i][digit] = idx-1;
nxt[i][s[i] - 'a'] = i;
for(int j = 0; j < 28; ++j)
maximize(nxt[i][j], nxt[idx-1][j]);
}
}
for(int j = 1; j < 17; ++j)
for(int i = 0; i <= n + 2; ++i) jump[j][i] = jump[j-1][jump[j-1][i]];
string ans = "";
string st;
vector<int> idx_st;
st.push_back('#');
idx_st.push_back(n + 2);
for(int i = 1; i <= n; ++i){
if (st.back() != s[i]){
idx_st.push_back(nxt[idx_st.back()][s[i] - 'a'] - 1);
st.push_back(s[i]);
ans.push_back('(');
}
else{
int idx = nxt[idx_st.back()][s[i] - 'a'];
int l = i, r = idx - 1;
bool check = true;
if (idx == -1 || l > r) check = false;
else{
for(int j = 16; j >= 0; --j) if (jump[j][r] >= l)
r = jump[j][r];
if (r != l) check = false;
}
if (check == false) {
st.pop_back();
idx_st.pop_back();
ans.push_back(')');
}
else{
st.push_back(s[i]);
idx_st.push_back(idx - 1);
ans.push_back('(');
}
}
// cout << "Nigger\n";
// cout << st << "\n";
// printArr(idx_st);
}
cout << ans << "\n";
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << " ms\n";
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... |