///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 3e5 + 10;
const ll SGM = 26;
ll n;
ll mtc[MXN][SGM];
string s, t;
bool Can(ll l, ll r){
if(l > r) return 1;
if(l == r) return 0;
if(s[l] == s[r]){
t[l] = '(', t[r] = ')';
return Can(l + 1, r - 1);
}
ll m = mtc[r][s[l] - 'a'];
if(m < l) return 0;
t[l] = '(', t[m] = ')';
return Can(l + 1, m - 1) && Can(m + 1, r);
}
int main(){
//ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> s, n = s.size();
s = "$" + s, t = s;
mtc[1][s[1] - 'a'] = 1;
for(int i = 2; i <= n; i ++){
ll c = s[i] - 'a';
for(int cp = 0; cp < 26; cp ++){
if(mtc[i - 1][c]){
mtc[i][cp] = mtc[mtc[i - 1][c] - 1][cp];
}
}
mtc[i][c] = i;
}
if(Can(1, n)) cout << t.substr(1, n) << '\n';
else cout << "-1\n";
return 0;
}
/*!
HE'S AN INSTIGATOR,
ENEMY ELIMINATOR,
AND WHEN HE KNOCKS YOU BETTER
YOU BETTER LET HIM IN.
*/
//! N.N
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
428 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
428 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
676 KB |
Output is correct |
8 |
Correct |
2 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1708 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
11 ms |
12876 KB |
Output is correct |
13 |
Correct |
15 ms |
13800 KB |
Output is correct |
14 |
Correct |
15 ms |
15020 KB |
Output is correct |
15 |
Correct |
16 ms |
16948 KB |
Output is correct |
16 |
Correct |
15 ms |
17080 KB |
Output is correct |
17 |
Correct |
19 ms |
18048 KB |
Output is correct |
18 |
Correct |
20 ms |
18976 KB |
Output is correct |
19 |
Correct |
19 ms |
20188 KB |
Output is correct |
20 |
Correct |
12 ms |
12884 KB |
Output is correct |
21 |
Correct |
22 ms |
21220 KB |
Output is correct |