#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
const int N = 1e5 + 5, A = 26;
int dp[N][A];
string ans, s;
void solve(int l, int r) {
if (l > r)
return;
int x = dp[r][s[l] - 'a'] - 1;
ans[l] = '(';
ans[x] = ')';
solve(l + 1, x - 1);
solve(x + 1, r);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
vector<char> stk;
for (char c : s) {
if (stk.empty() || stk.back() != c) {
stk.push_back(c);
}
else {
stk.pop_back();
}
}
if (!stk.empty()) {
cout << "-1\n";
return 0;
}
int n = s.size();
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
dp[i][s[i] - 'a'] = i + 1;
if (i > 0 && dp[i - 1][s[i] - 'a'] > 1) {
for (int j = 0; j < A; j++) {
dp[i][j] = max(dp[i][j], dp[dp[i - 1][s[i] - 'a'] - 2][j]);
}
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < 2; j++) {
// cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
// cout << dp[4][1] << "\n";
// return 0;
ans.resize(n);
solve(0, n - 1);
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
10 ms |
10496 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
11 ms |
10496 KB |
Output is correct |
7 |
Correct |
10 ms |
10496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
10 ms |
10496 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
11 ms |
10496 KB |
Output is correct |
7 |
Correct |
10 ms |
10496 KB |
Output is correct |
8 |
Correct |
10 ms |
10496 KB |
Output is correct |
9 |
Correct |
11 ms |
10624 KB |
Output is correct |
10 |
Correct |
11 ms |
10624 KB |
Output is correct |
11 |
Correct |
11 ms |
10624 KB |
Output is correct |
12 |
Correct |
14 ms |
11264 KB |
Output is correct |
13 |
Correct |
16 ms |
11392 KB |
Output is correct |
14 |
Correct |
16 ms |
11648 KB |
Output is correct |
15 |
Correct |
20 ms |
11904 KB |
Output is correct |
16 |
Correct |
18 ms |
11904 KB |
Output is correct |
17 |
Correct |
17 ms |
11904 KB |
Output is correct |
18 |
Correct |
20 ms |
11264 KB |
Output is correct |
19 |
Correct |
17 ms |
11648 KB |
Output is correct |
20 |
Correct |
14 ms |
11392 KB |
Output is correct |
21 |
Correct |
19 ms |
11904 KB |
Output is correct |