이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |