結果

問題 No.672 最長AB列
ユーザー htensaihtensai
提出日時 2019-11-12 08:42:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 869 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 79,260 KB
実行使用メモリ 104,804 KB
最終ジャッジ日時 2023-10-17 14:04:59
合計ジャッジ時間 8,745 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
57,368 KB
testcase_01 AC 124 ms
57,316 KB
testcase_02 AC 124 ms
57,164 KB
testcase_03 AC 123 ms
57,016 KB
testcase_04 AC 124 ms
57,292 KB
testcase_05 AC 125 ms
57,252 KB
testcase_06 AC 125 ms
57,240 KB
testcase_07 AC 126 ms
57,156 KB
testcase_08 AC 122 ms
57,180 KB
testcase_09 AC 484 ms
104,804 KB
testcase_10 AC 431 ms
82,452 KB
testcase_11 AC 429 ms
82,208 KB
testcase_12 AC 416 ms
72,072 KB
testcase_13 AC 409 ms
70,336 KB
testcase_14 AC 403 ms
69,688 KB
testcase_15 AC 403 ms
71,376 KB
testcase_16 AC 397 ms
69,872 KB
testcase_17 AC 456 ms
89,280 KB
testcase_18 AC 370 ms
69,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		char[] arr = sc.next().toCharArray();
		int[] sums = new int[arr.length + 1];
		HashMap<Integer, TreeSet<Integer>> map = new HashMap<>();
		TreeSet<Integer> first = new TreeSet<>();
		first.add(0);
		map.put(0, first);
		for (int i = 1; i <= arr.length; i++) {
		    sums[i] = sums[i - 1];
		    if (arr[i - 1] == 'A') {
		        sums[i]++;
		    } else {
		        sums[i]--;
		    }
		    if (map.containsKey(sums[i])) {
		        map.get(sums[i]).add(i);
		    } else {
		        TreeSet<Integer> set = new TreeSet<>();
		        set.add(i);
		        map.put(sums[i], set);
		    }
		}
		int max = 0;
		for (TreeSet<Integer> set : map.values()) {
		    max = Math.max(max, set.last() - set.first());
		}
		System.out.println(max);
   }
}

0