# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
62353 | kingpig9 | 괄호 문자열 (CEOI16_match) | C++11 | 25 ms | 12732 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//This is so hard to understand why it even works...
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5 + 10;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
int N;
char S[MAXN];
char ans[MAXN];
int prv[MAXN][26];
void rec (int lt, int rt) {
if (lt > rt) {
return;
}
int match = prv[rt][S[lt] - 'a']; //which index you match with?
if (match <= lt) {
//then cannot match
puts("-1");
exit(0);
}
ans[lt] = '(';
ans[match] = ')';
rec(lt + 1, match - 1);
rec(match + 1, rt);
}
int main() {
if (fopen("match.in", "r")) {
freopen("match.in", "r", stdin);
freopen("match.out", "w", stdout);
}
scanf("%s", S);
N = strlen(S);
for (int i = 0; i < N; i++) {
for (int j = 0; j < 26; j++) {
prv[i][j] = -1;
if (j == S[i] - 'a') {
prv[i][j] = i;
} else if (i) {
if (prv[i - 1][S[i] - 'a'] > 0) {
prv[i][j] = prv[prv[i - 1][S[i] - 'a'] - 1][j];
}
}
}
}
rec(0, N - 1);
puts(ans);
}
컴파일 시 표준 에러 (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... |