結果

問題 No.1894 Delete AB
ユーザー tktk_snsntktk_snsn
提出日時 2022-04-08 21:53:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 648 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 10,792 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2023-08-19 00:31:30
合計ジャッジ時間 2,066 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
7,948 KB
testcase_01 AC 33 ms
14,684 KB
testcase_02 AC 33 ms
14,416 KB
testcase_03 AC 103 ms
7,796 KB
testcase_04 AC 104 ms
7,912 KB
testcase_05 AC 107 ms
7,864 KB
testcase_06 AC 105 ms
7,908 KB
testcase_07 AC 106 ms
7,848 KB
testcase_08 AC 111 ms
7,860 KB
testcase_09 AC 32 ms
8,292 KB
testcase_10 AC 36 ms
8,364 KB
testcase_11 AC 28 ms
8,196 KB
testcase_12 AC 27 ms
8,780 KB
testcase_13 AC 31 ms
15,488 KB
testcase_14 AC 43 ms
14,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    N = int(input())
    S = list(input())
    S.append("?")

    prev = [-1] * N
    stack = []
    for i in range(N):
        if S[i] == "A":
            stack.append(i)
        else:
            if stack:
                prev[i] = stack.pop()

    ans = [N]
    i = N - 1
    while i >= 0:
        if prev[i] == -1:
            ans.append(i)
            i -= 1
        else:
            if S[ans[-1]] != "B":
                ans.append(i)
                i -= 1
            else:
                i = prev[i] - 1

    ans.reverse()
    ans.pop()
    print("".join(S[i] for i in ans))


T = int(input())
for _ in range(T):
    solve()
0