This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int gde[N][26];
int ans[N];
string s;
void Resi(int l, int r) {
if (l > r) return;
int mid = gde[r][s[l] - 'a'];
ans[mid] = 1;
Resi(l + 1, mid - 1);
Resi(mid + 1, r);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
int n = s.size();
stack<char> stk;
for (int i = 0; i < n; i++) {
if (!stk.empty() && stk.top() == s[i]) stk.pop();
else stk.push(s[i]);
}
if (!stk.empty()) {
cout << -1 << en;
return 0;
}
for (int i = 0; i < 26 * n; i++) gde[i / 26][i % 26] = -1;
gde[0][s[0] - 'a'] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 26; j++) {
if (s[i] - 'a' == j) gde[i][j] = i;
else if (gde[i - 1][s[i] - 'a'] > 0) gde[i][j] = gde[gde[i - 1][s[i] - 'a'] - 1][j];
}
}
Resi(0, n - 1);
for (int i = 0; i < n; i++) {
if (ans[i] == 0) cout << '(';
else cout << ')';
}
cout << en;
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... |