結果

問題 No.1894 Delete AB
ユーザー Shirotsume
提出日時 2022-02-25 12:40:34
言語 Java
(openjdk 23)
結果
AC  
実行時間 571 ms / 2,000 ms
コード長 892 bytes
コンパイル時間 2,735 ms
コンパイル使用メモリ 75,516 KB
実行使用メモリ 52,572 KB
最終ジャッジ日時 2024-09-15 23:43:54
合計ジャッジ時間 9,092 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {
	  public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int t = sc.nextInt();
	        for(int x = 0; x < t; x++){
	        	int n = sc.nextInt();
	        	String s = sc.next();
	        	Deque<String> q = new ArrayDeque<>();
	        	for(int i = n - 1; i >= 0; i--){
	        		q.addFirst(s.substring(i, i + 1));
	        	if(q.size() >= 3){
	        		String a1 = q.pollFirst();
	        		String a2 = q.pollFirst();
	        		String a3 = q.pollFirst();
	        		if(a1.equals("A") && a2.equals("B") && a3.equals("B")){q.addFirst(a3);}
	        		else{
	        			q.addFirst(a3);
	        			q.addFirst(a2);
	        			q.addFirst(a1);
	        		}
	        	}
	        }
	        System.out.println(String.join("", q));
	        }

	    }
}
0