結果

問題 No.672 最長AB列
ユーザー バカらっくバカらっく
提出日時 2018-04-20 05:22:36
言語 Swift
(5.10.0)
結果
TLE  
実行時間 -
コード長 887 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 107,672 KB
実行使用メモリ 18,056 KB
最終ジャッジ日時 2023-08-20 10:41:46
合計ジャッジ時間 4,466 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
12,216 KB
testcase_01 AC 3 ms
7,848 KB
testcase_02 AC 3 ms
7,676 KB
testcase_03 AC 3 ms
7,584 KB
testcase_04 AC 3 ms
7,860 KB
testcase_05 AC 4 ms
7,800 KB
testcase_06 AC 4 ms
7,668 KB
testcase_07 AC 3 ms
7,720 KB
testcase_08 AC 3 ms
7,704 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
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