using namespace std;
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#define MAXN 100005
#define MOD 998244353
int N,pre0[MAXN],pre1[MAXN],mask[MAXN];
string s;
char ans[MAXN];
map<long long,vector<int>> pos[26];
bool solve(int l,int r) {
if(l > r) return true;
auto uwu = pos[s[l] - 'a'][mask[l]];
auto it = upper_bound(uwu.begin(),uwu.end(),r);
if(it == uwu.begin() || *prev(it) <= l) return false;
int owo = *prev(it);
// cout << l << " " << owo << endl;
ans[l] = '(';
ans[owo] = ')';
if(l == r - 1) return true;
return solve(l + 1,owo - 1) & solve(owo + 1,r);
}
int main() {
cin >> s;
N = s.length();
long long uwu = 0;
mask[0] = 0;
stack<int> st;
for(int i = 0;i < N;++i) {
if(st.size() && s[i] == s[st.top()]) {
mask[i + 1] = (mask[i] + MOD - (st.size() * (1 << (s[st.top()] - 'a')) % MOD)) % MOD;
st.pop();
}else{
st.push(i);
mask[i + 1] = (mask[i] + st.size() * (1 << (s[i] - 'a'))) % MOD;
}
pos[s[i] - 'a'][mask[i + 1]].push_back(i);
}
if(solve(0,N - 1)) {
for(int i = 0;i < N;++i) {
cout << ans[i];
}
cout << endl;
}else{
cout << -1 << endl;
}
return 0;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:33:12: warning: unused variable 'uwu' [-Wunused-variable]
33 | long long uwu = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
1100 KB |
Output is correct |
10 |
Correct |
3 ms |
1208 KB |
Output is correct |
11 |
Correct |
3 ms |
1464 KB |
Output is correct |
12 |
Correct |
35 ms |
9272 KB |
Output is correct |
13 |
Correct |
44 ms |
10092 KB |
Output is correct |
14 |
Correct |
47 ms |
11616 KB |
Output is correct |
15 |
Correct |
29 ms |
38576 KB |
Output is correct |
16 |
Correct |
29 ms |
38588 KB |
Output is correct |
17 |
Correct |
29 ms |
10092 KB |
Output is correct |
18 |
Correct |
19 ms |
9500 KB |
Output is correct |
19 |
Correct |
68 ms |
13296 KB |
Output is correct |
20 |
Correct |
34 ms |
10056 KB |
Output is correct |
21 |
Correct |
75 ms |
14344 KB |
Output is correct |