結果

問題 No.672 最長AB列
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-05-18 01:22:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 72,652 KB
実行使用メモリ 76,312 KB
最終ジャッジ日時 2023-09-10 22:47:16
合計ジャッジ時間 6,824 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,868 KB
testcase_01 AC 114 ms
55,600 KB
testcase_02 AC 114 ms
55,688 KB
testcase_03 AC 115 ms
56,040 KB
testcase_04 AC 113 ms
55,996 KB
testcase_05 AC 113 ms
55,468 KB
testcase_06 AC 113 ms
55,460 KB
testcase_07 AC 114 ms
55,700 KB
testcase_08 AC 114 ms
55,564 KB
testcase_09 AC 319 ms
76,312 KB
testcase_10 AC 302 ms
67,012 KB
testcase_11 AC 300 ms
67,184 KB
testcase_12 AC 257 ms
59,644 KB
testcase_13 AC 261 ms
59,180 KB
testcase_14 AC 264 ms
59,156 KB
testcase_15 AC 258 ms
58,864 KB
testcase_16 AC 252 ms
59,256 KB
testcase_17 AC 288 ms
65,928 KB
testcase_18 AC 247 ms
58,872 KB
権限があれば一括ダウンロードができます

ソースコード

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);
				ar[i+1] = numA-numB;
			}
			else {
				numB++;
//				ar[i+1] = Math.abs(numA-numB);
				ar[i+1] = numA-numB;
			}
		}
		
//		for (int i=1; i<s.length(); i++) {
//			ar[i] += ar[i-1];
//		}
		
		//0について調べるなら、一番左端の0と一番右端の0のみ考えればよい。
		Map<Integer,Integer> map = new HashMap<>();
		int max = 0;
		for (int i=0; i<n+1; i++) {
			if (map.containsKey(ar[i])) {
				max = Math.max(max,Math.abs(map.get(ar[i])-i));
			}
			else {
				map.put(ar[i],i);
			}
		}
		
		out.println(max);
//		map.forEach((k,v)->out.println(k+":"+v));
//		for (int i=0; i<n+1; i++) {
//			out.print((i==0?"":" ")+ar[i]);
//		}
	}
}
0