結果

問題 No.672 最長AB列
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-05-18 01:15:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 728 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 80,148 KB
実行使用メモリ 76,368 KB
最終ジャッジ日時 2023-09-10 22:46:29
合計ジャッジ時間 7,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
55,516 KB
testcase_01 AC 114 ms
55,728 KB
testcase_02 AC 113 ms
54,192 KB
testcase_03 AC 113 ms
55,364 KB
testcase_04 AC 122 ms
55,272 KB
testcase_05 AC 114 ms
55,356 KB
testcase_06 WA -
testcase_07 AC 114 ms
56,144 KB
testcase_08 WA -
testcase_09 AC 326 ms
76,368 KB
testcase_10 AC 342 ms
69,244 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import static java.lang.System.*;
import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		String s = sc.next();
		int n = s.length();
		int[] ar = new int[n+1];
		int numA = 0;
		int numB = 0;
		for (int i=0; i<n; i++) {
			if (s.charAt(i)=='A') {
				numA++;
				ar[i+1] = Math.abs(numA-numB);
			}
			else {
				numB++;
				ar[i+1] = Math.abs(numA-numB);
			}
		}
		
		Map<Integer,Integer> map = new HashMap<>();
		int max = 0;
		for (int i=0; i<n+1; i++) {
			if (map.containsKey(ar[i])) {
				map.put(ar[i],Math.abs(map.get(ar[i])-i));
				max = Math.max(max,map.get(ar[i]));
			}
			else {
				map.put(ar[i],i);
			}
		}
		
		out.println(max);
	}
} 
0