結果

問題 No.1894 Delete AB
ユーザー ripity
提出日時 2022-04-14 19:37:07
言語 Java
(openjdk 23)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 4,475 ms
コンパイル使用メモリ 79,084 KB
実行使用メモリ 51,148 KB
最終ジャッジ日時 2024-12-24 10:30:56
合計ジャッジ時間 10,217 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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