#include <bits/stdc++.h>
using namespace std;
int last[100005][30];
int pas[100005];
string s;
int n;
void rek(int l, int r) {
// cout << l << ' ' << r << endl;
if (l > r) return;
if (l == r) assert(0);
if (l == r - 1) {
assert(s[l] == s[r]);
pas[l] = r;
return;
}
int tmp = last[r][s[l] - 'a'];
pas[l] = tmp;
rek(l + 1, tmp - 1);
rek(tmp + 1, r);
}
int main() {
cin >> s;
n = s.length();
memset(last, -1, sizeof last);
vector<int> a(n);
for (int i = 0; i < n; ++i) a[i] = (s[i] - 'a');
stack<int> st;
for (int i = 0; i < n; ++i) {
if (st.empty() or st.top() != a[i]) {
st.push(a[i]);
} else {
st.pop();
}
}
// cout << st.size() << endl;
if (!st.empty()) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < n; ++i) {
int cur = (s[i] - 'a');
int prev = -1;
if (i) {
prev = last[i - 1][cur];
}
last[i][cur] = i;
// cout << prev << endl;
if (prev > 0) {
for (int j = 0; j < 26; ++j) {
if (j == cur) continue;
last[i][j] = last[prev - 1][j];
}
}
}
// cout << last[4][0] << endl;
memset(pas, -1, sizeof pas);
rek(0, n - 1);
string ans(n, '?');
for (int i = 0; i < n; ++i) {
if (pas[i] == -1) continue;
ans[i] = '(';
ans[pas[i]] = ')';
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12372 KB |
Output is correct |
2 |
Correct |
6 ms |
12364 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12372 KB |
Output is correct |
2 |
Correct |
6 ms |
12364 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
5 ms |
12356 KB |
Output is correct |
5 |
Correct |
5 ms |
12340 KB |
Output is correct |
6 |
Correct |
5 ms |
12500 KB |
Output is correct |
7 |
Correct |
5 ms |
12372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12372 KB |
Output is correct |
2 |
Correct |
6 ms |
12364 KB |
Output is correct |
3 |
Correct |
5 ms |
11988 KB |
Output is correct |
4 |
Correct |
5 ms |
12356 KB |
Output is correct |
5 |
Correct |
5 ms |
12340 KB |
Output is correct |
6 |
Correct |
5 ms |
12500 KB |
Output is correct |
7 |
Correct |
5 ms |
12372 KB |
Output is correct |
8 |
Correct |
6 ms |
12372 KB |
Output is correct |
9 |
Correct |
7 ms |
12500 KB |
Output is correct |
10 |
Correct |
6 ms |
12500 KB |
Output is correct |
11 |
Correct |
6 ms |
12604 KB |
Output is correct |
12 |
Correct |
10 ms |
14088 KB |
Output is correct |
13 |
Correct |
9 ms |
14420 KB |
Output is correct |
14 |
Correct |
10 ms |
14804 KB |
Output is correct |
15 |
Correct |
14 ms |
15540 KB |
Output is correct |
16 |
Correct |
12 ms |
15556 KB |
Output is correct |
17 |
Correct |
12 ms |
15648 KB |
Output is correct |
18 |
Correct |
12 ms |
14144 KB |
Output is correct |
19 |
Correct |
13 ms |
14784 KB |
Output is correct |
20 |
Correct |
10 ms |
14400 KB |
Output is correct |
21 |
Correct |
13 ms |
15316 KB |
Output is correct |