# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
128033 | PeppaPig | Match (CEOI16_match) | C++14 | 59 ms | 61432 KiB |
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>
using namespace std;
const int N = 1e5+5;
int n, dep[N];
char a[N], ans[N];
stack<int> S;
vector<int> id[N][26];
void solve(int l, int r) {
if(l > r) return;
vector<int> &v = id[dep[l]][a[l] - 'a'];
auto it = --lower_bound(v.begin(), v.end(), r + 1);
ans[l] = '(', ans[*it] = ')';
solve(l + 1, *it - 1), solve(*it + 1, r);
}
int main() {
scanf("%s", a + 1);
n = strlen(a + 1);
for(int i = 1; i <= n; i++) {
if(S.empty() || a[S.top()] != a[i]) {
dep[i] = S.size() + 1;
S.emplace(i);
} else {
dep[i] = dep[S.top()];
S.pop();
}
}
for(int i = 1; i <= n; i++) id[dep[i]][a[i] - 'a'].emplace_back(i);
if(S.size()) return !printf("-1\n");
else solve(1, n);
printf("%s\n", ans + 1);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |