結果

問題 No.1894 Delete AB
ユーザー ripityripity
提出日時 2022-04-14 19:37:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 76,244 KB
実行使用メモリ 63,860 KB
最終ジャッジ日時 2023-08-25 22:56:30
合計ジャッジ時間 8,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
55,812 KB
testcase_01 AC 228 ms
59,120 KB
testcase_02 AC 240 ms
59,392 KB
testcase_03 AC 313 ms
60,732 KB
testcase_04 AC 322 ms
61,228 KB
testcase_05 AC 332 ms
60,744 KB
testcase_06 AC 332 ms
61,272 KB
testcase_07 AC 340 ms
61,068 KB
testcase_08 AC 347 ms
59,572 KB
testcase_09 AC 243 ms
59,384 KB
testcase_10 AC 251 ms
59,120 KB
testcase_11 AC 230 ms
60,800 KB
testcase_12 AC 271 ms
59,084 KB
testcase_13 AC 226 ms
60,892 KB
testcase_14 AC 234 ms
63,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    
    public static Scanner sc = new Scanner(System.in);
    public static PrintWriter pw = new PrintWriter(System.out);
    
    public static void main(String[] args) {
        
        int t = sc.nextInt();
        while( t > 0 ) {
            solve();
            t--;
        }
        
        pw.flush();
        
    }
    
    static void solve() {
        
        int N = sc.nextInt();
        char[] s = sc.next().toCharArray();
        LinkedList<Character> stc = new LinkedList<>();
        
        for( int i = 0; i < N; i++ ) {
            if( s[i] == 'A' ) {
                stc.push('A');
            }else {
                while( stc.size() >= 2 ) {
                    char fst = stc.pop();
                    char scd = stc.pop();
                    if( !( fst == 'B' && scd == 'A' ) ) {
                        stc.push(scd);
                        stc.push(fst);
                        break;
                    }
                }
                stc.push('B');
            }
        }
        
        Collections.reverse(stc);
        
        for( char c : stc ) pw.print(c);
        pw.println();
        
    }
    
}
0