結果

問題 No.672 最長AB列
ユーザー バカらっくバカらっく
提出日時 2018-04-20 05:23:23
言語 Swift
(5.10.0)
結果
TLE  
実行時間 -
コード長 889 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 108,736 KB
実行使用メモリ 20,924 KB
最終ジャッジ日時 2023-08-20 10:42:03
合計ジャッジ時間 7,002 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
20,924 KB
testcase_01 AC 3 ms
7,424 KB
testcase_02 AC 3 ms
7,444 KB
testcase_03 AC 4 ms
7,468 KB
testcase_04 AC 3 ms
7,424 KB
testcase_05 AC 3 ms
7,360 KB
testcase_06 AC 3 ms
7,488 KB
testcase_07 AC 3 ms
7,428 KB
testcase_08 AC 4 ms
7,424 KB
testcase_09 AC 25 ms
13,996 KB
testcase_10 AC 25 ms
14,048 KB
testcase_11 AC 25 ms
14,004 KB
testcase_12 AC 1,188 ms
14,192 KB
testcase_13 AC 932 ms
14,076 KB
testcase_14 AC 26 ms
14,216 KB
testcase_15 AC 119 ms
14,068 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

let inpt = readLine()!.map{ $0 }
var aCount = [0]
var bCount = [0]
if inpt.first! == "A" {
    aCount[0] = 1
} else {
    bCount[0] = 1
}

for i in(1..<inpt.count) {
    aCount.append(aCount.last!)
    bCount.append(bCount.last!)
    if inpt[i] == "A" {
        aCount[i] = aCount[i] + 1
    } else {
        bCount[i] = bCount[i] + 1
    }
}

var ans = 0
for i in (0..<inpt.count) {
    var j = inpt.count - 1
    if (j-i) % 2 == 0 {
        j -= 1
    }
    while j > i {
        let len = j - i + 1
        if(len <= ans) {
            break
        }
        var aCnt = aCount[j]
        var bCnt = bCount[j]
        if(i>0) {
            aCnt -= aCount[i-1]
            bCnt -= bCount[i-1]
        }
        if(aCnt <= 0 || bCnt <= 0) {
            break
        }
        if aCnt == bCnt {
            ans = max(ans, len)
            break
        }
        j -= 2
    }
}
print(ans)
0