# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202300 | quocnguyen1012 | Match (CEOI16_match) | C++14 | 21 ms | 12148 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>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int N;
int last[maxn][26];
string str;
char res[maxn];
void match(int l, int r)
{
if (l > r) return;
if (l % 2 == r % 2){
cout << -1;
exit(0);
}
int mid = last[r][str[l] - 'a'];
res[l] = '('; res[mid] = ')';
match(l + 1, mid - 1);
match(mid + 1, r);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> str; N = str.size();
str = " " + str;
for (int i = 0; i < 26; ++i){
last[0][i] = 1;
}
for (int i = 1; i <= N; ++i){
for (int j = 0; j < 26; ++j){
if (str[i] - 'a' == j){
last[i][j] = i;
}
else{
last[i][j] = last[last[i - 1][str[i] - 'a'] - 1][j];
}
}
}
match(1, N);
for (int i = 1; i <= N; ++i){
cout << res[i];
}
}
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... |