結果

問題 No.672 最長AB列
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-05-18 01:17:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 923 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 75,016 KB
実行使用メモリ 78,576 KB
最終ジャッジ日時 2023-09-10 22:46:42
合計ジャッジ時間 7,523 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,560 KB
testcase_01 AC 119 ms
55,620 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 119 ms
55,416 KB
testcase_05 AC 116 ms
55,452 KB
testcase_06 AC 117 ms
53,312 KB
testcase_07 AC 117 ms
55,384 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 306 ms
67,484 KB
testcase_11 AC 286 ms
63,464 KB
testcase_12 WA -
testcase_13 AC 262 ms
58,816 KB
testcase_14 WA -
testcase_15 AC 263 ms
59,008 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 249 ms
58,416 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);
			}
			else {
				numB++;
				ar[i+1] = Math.abs(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 = Integer.MIN_VALUE;
		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));
	}
}
0