結果

問題 No.672 最長AB列
ユーザー uafr_csuafr_cs
提出日時 2018-04-13 22:32:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 80,900 KB
実行使用メモリ 44,924 KB
最終ジャッジ日時 2023-09-10 00:04:16
合計ジャッジ時間 7,503 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
39,160 KB
testcase_01 AC 110 ms
39,472 KB
testcase_02 AC 111 ms
39,292 KB
testcase_03 AC 110 ms
39,416 KB
testcase_04 AC 110 ms
39,472 KB
testcase_05 AC 108 ms
39,676 KB
testcase_06 AC 109 ms
39,088 KB
testcase_07 AC 107 ms
39,400 KB
testcase_08 AC 110 ms
39,784 KB
testcase_09 AC 244 ms
44,924 KB
testcase_10 AC 241 ms
43,336 KB
testcase_11 AC 242 ms
43,752 KB
testcase_12 AC 253 ms
42,344 KB
testcase_13 AC 252 ms
43,128 KB
testcase_14 AC 241 ms
43,000 KB
testcase_15 AC 251 ms
43,308 KB
testcase_16 AC 255 ms
43,204 KB
testcase_17 AC 253 ms
44,028 KB
testcase_18 AC 247 ms
43,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static final long MOD = 1000000007;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final char[] chs = sc.next().toCharArray();
		int[] score = new int[chs.length];
		for(int i = 0; i < chs.length; i++){
			if(chs[i] == 'A'){
				score[i] = 1;
			}else{
				score[i] = -1;
			}
		}
		
		for(int i = 1; i < score.length; i++){
			score[i] += score[i - 1];
		}
		
		final int min = Math.min(0, Arrays.stream(score).min().getAsInt());
		final int max = Math.max(0, Arrays.stream(score).max().getAsInt());
		final int size = max - min + 1;
		int[] prev = new int[size];
		Arrays.fill(prev, score.length);
		prev[0 - min] = -1;
		
		int answer = 0;
		for(int i = 0; i < score.length; i++){
			final int s = score[i] - min;
			if(prev[s] < score.length){
				answer = Math.max(answer, i - prev[s]);
			}else{
				prev[s] = i;
			}
		}
		
		System.out.println(answer);
	}
}
0