結果

問題 No.672 最長AB列
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2018-04-13 23:40:22
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 904 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 71,756 KB
実行使用メモリ 62,980 KB
最終ジャッジ日時 2023-09-10 04:08:32
合計ジャッジ時間 6,847 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
60,348 KB
testcase_01 AC 114 ms
55,984 KB
testcase_02 AC 114 ms
57,904 KB
testcase_03 AC 115 ms
55,984 KB
testcase_04 WA -
testcase_05 AC 117 ms
55,536 KB
testcase_06 AC 118 ms
55,684 KB
testcase_07 AC 115 ms
55,960 KB
testcase_08 AC 116 ms
55,824 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int ans = function(s);
        System.out.println(ans);
    }
    
    static int function(String s) {
        int len = s.length();
        int [] num = new int [len];
        if(s.charAt(0) == 'A') {
            num[0] = 1;
        } else {
            num[0] = -1;
        }
        for(int i = 1; i < len; i++) {
            if(s.charAt(i) == 'A') {
                num[i] = num[i-1] + 1;
            } else {
                num[i] = num[i-1] - 1;
            }
        }
        int max = 0;
        for(int i = 0; i < len; i++) {
            for(int j = i; j < len; j++) {
                if(num[j]-num[i] == 0) {
                    max = Math.max(max,j-i);
                }
            }
        }
        return max;
    }
}
0