이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, S = 26;
char str[N], ant[N];
int dp[S][N];
int n;
static void solve(int l, int r) {
if (l > r)
return;
int match = dp[str[l] - 'a'][r];
if (!match)
throw -1;
ant[l] = '(';
ant[match] = ')';
solve(l + 1, match - 1);
solve(match + 1, r); }
int main() {
#ifdef HOME
freopen("match.in", "r", stdin);
freopen("match.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
vector<int> stk;
stk.reserve(N);
cin >> (str + 1);
n = strlen(str + 1);
for (int i = 1; i <= n; ++i) {
dp[str[i] - 'a'][i] = i;
if (i == 1)
continue;
int pos = dp[str[i] - 'a'][i - 1];
if (pos <= 1)
continue;
for (int ch = 0; ch < S; ++ch) {
if (str[i] != ch + 'a')
dp[ch][i] = dp[ch][pos - 1]; } }
try {
solve(1, n); }
catch (int e) {
cout << "-1\n";
return 0; }
cout << (ant + 1) << endl;
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... |