結果
| 問題 | No.3503 Brackets Stack Query 2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 18:10:26 |
| 言語 | C (gcc 15.2.0) |
| 結果 |
AC
|
| 実行時間 | 79 ms / 2,000 ms |
| コード長 | 2,360 bytes |
| 記録 | |
| コンパイル時間 | 1,671 ms |
| コンパイル使用メモリ | 36,096 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-04-18 18:10:43 |
| 合計ジャッジ時間 | 7,335 ms |
|
ジャッジサーバーID (参考情報) |
judge3_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 30 |
ソースコード
#include <stdio.h>
#include <string.h>
#define MAXQ 800005
static char char_stack[MAXQ];
static int char_top = 0;
/*
* frame_stack: parser frame stack
* 0 = this '(' is waiting for its '|'
* 1 = this '(' has seen its '|', waiting for ')'
*/
static int frame_stack[MAXQ];
static int frame_top = 0;
/*
* error_stack[i] = 1 if the string is invalid after i characters.
* error_stack[0] = 0 (empty string is always valid).
*/
static char error_stack[MAXQ];
static int error_top = 1;
int main(void) {
error_stack[0] = 0;
int Q;
scanf("%d\n", &Q);
while (Q--) {
int type;
scanf("%d", &type);
if (type == 1) {
char c;
scanf(" %c", &c);
char_stack[char_top++] = c;
int cur_err = error_stack[error_top - 1];
if (cur_err) {
error_stack[error_top++] = 1;
} else {
if (c == '(') {
frame_stack[frame_top++] = 0;
error_stack[error_top++] = 0;
} else if (c == '|') {
if (frame_top == 0 || frame_stack[frame_top - 1] == 1) {
error_stack[error_top++] = 1;
} else {
frame_stack[frame_top - 1] = 1;
error_stack[error_top++] = 0;
}
} else { /* ')' */
if (frame_top == 0 || frame_stack[frame_top - 1] == 0) {
error_stack[error_top++] = 1;
} else {
frame_top--;
error_stack[error_top++] = 0;
}
}
}
} else { /* type == 2 */
char c = char_stack[--char_top];
int was_err_this = error_stack[--error_top];
if (!was_err_this) {
/* undo the frame_stack change made by this character */
if (c == '(') {
frame_top--;
} else if (c == '|') {
frame_stack[frame_top - 1] = 0;
} else { /* ')' */
frame_stack[frame_top++] = 1;
}
}
}
int valid = !error_stack[error_top - 1] && frame_top == 0;
puts(valid ? "Yes" : "No");
}
return 0;
}