結果

問題 No.672 最長AB列
ユーザー fal_rndfal_rnd
提出日時 2018-04-13 23:54:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 523 ms / 2,000 ms
コード長 574 bytes
コンパイル時間 3,045 ms
コンパイル使用メモリ 82,628 KB
実行使用メモリ 91,404 KB
最終ジャッジ日時 2023-09-10 04:15:08
合計ジャッジ時間 9,341 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
56,196 KB
testcase_01 AC 132 ms
55,508 KB
testcase_02 AC 128 ms
55,820 KB
testcase_03 AC 128 ms
55,512 KB
testcase_04 AC 130 ms
55,936 KB
testcase_05 AC 128 ms
56,100 KB
testcase_06 AC 130 ms
55,760 KB
testcase_07 AC 131 ms
56,140 KB
testcase_08 AC 132 ms
56,180 KB
testcase_09 AC 523 ms
91,404 KB
testcase_10 AC 434 ms
82,096 KB
testcase_11 AC 482 ms
79,780 KB
testcase_12 AC 324 ms
59,188 KB
testcase_13 AC 327 ms
59,132 KB
testcase_14 AC 318 ms
58,916 KB
testcase_15 AC 338 ms
59,132 KB
testcase_16 AC 318 ms
59,336 KB
testcase_17 AC 423 ms
77,008 KB
testcase_18 AC 292 ms
59,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main{
	public static void main(String[] $){
		Scanner s=new Scanner(System.in);
		char[]v=s.next().toCharArray();
		int n=v.length;
		int[]d=new int[n+1];
		for(int i=0;i<n;++i)
			d[i+1]=d[i]+(v[i]=='A'?1:-1);

		Map<Integer,Integer>left=new HashMap<>(),right=new HashMap<>();
		for(int i=0;i<n+1;++i) {
			left.merge(d[i],i,Math::min);
			right.merge(d[i],i,Math::max);
		}
		
		System.out.println(
				right.keySet().stream()
				.map(i->right.get(i)-left.getOrDefault(i,Integer.MAX_VALUE))
				.mapToInt(o->o)
				.max().getAsInt()
				);
	}
}
0