結果

問題 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  
実行時間 142 ms / 2,000 ms
コード長 648 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 18,132 KB
最終ジャッジ日時 2024-05-06 07:28:16
合計ジャッジ時間 1,968 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,752 KB
testcase_01 AC 50 ms
17,112 KB
testcase_02 AC 51 ms
16,984 KB
testcase_03 AC 136 ms
10,752 KB
testcase_04 AC 142 ms
10,752 KB
testcase_05 AC 133 ms
10,624 KB
testcase_06 AC 134 ms
10,752 KB
testcase_07 AC 136 ms
10,624 KB
testcase_08 AC 142 ms
10,624 KB
testcase_09 AC 47 ms
10,752 KB
testcase_10 AC 52 ms
10,752 KB
testcase_11 AC 45 ms
10,752 KB
testcase_12 AC 43 ms
11,376 KB
testcase_13 AC 49 ms
18,132 KB
testcase_14 AC 63 ms
16,980 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