이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
string s;
int n, opt[100000][26];
char ans[100000];
string solve(int l = 0, int r = n - 1) {
if (l > r) return "";
return '(' + solve(l + 1, opt[r][s[l] - 'a'] - 1) + ')' + solve(opt[r][s[l] - 'a'] + 1, r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
stack<char> stck;
FOR(i, 0, n) {
if (stck.size() && s[i] == stck.top()) stck.pop();
else stck.push(s[i]);
FOR(j, 0, 26) {
if (j == s[i] - 'a') opt[i][j] = i;
else if (i && opt[i - 1][s[i] - 'a']) opt[i][j] = opt[opt[i - 1][s[i] - 'a'] - 1][j];
else opt[i][j] = -1;
}
}
if (stck.size()) return cout << -1, 0;
cout << solve();
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... |