結果

問題 No.672 最長AB列
ユーザー 0w10w1
提出日時 2018-04-13 22:34:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 724 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 210,504 KB
実行使用メモリ 23,652 KB
最終ジャッジ日時 2023-09-10 04:12:20
合計ジャッジ時間 4,064 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 242 ms
23,652 KB
testcase_10 AC 178 ms
14,344 KB
testcase_11 AC 161 ms
14,428 KB
testcase_12 AC 18 ms
5,020 KB
testcase_13 AC 19 ms
5,020 KB
testcase_14 AC 18 ms
5,196 KB
testcase_15 AC 19 ms
5,044 KB
testcase_16 AC 18 ms
5,096 KB
testcase_17 AC 170 ms
14,432 KB
testcase_18 AC 7 ms
5,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

signed main() {
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  vector<int> pa(S.size() + 1), pb(S.size() + 1);
  map<int, int> minpos, maxpos;
  minpos[0] = maxpos[0] = 0;
  for (int i = 0; i < S.size(); ++i) {
    pa[i + 1] = pa[i] + (S[i] == 'A');
    pb[i + 1] = pb[i] + (S[i] == 'B');
    int d = pa[i + 1] - pb[i + 1];
    if (minpos.count(d)) minpos[d] = min(minpos[d], i + 1);
    else minpos[d] = i + 1;
    maxpos[d] = max(maxpos[d], i + 1);
  }
  int ans = 0;
  for (auto it = minpos.begin(); it != minpos.end(); ++it) {
    if (maxpos.count(it->first)) {
      ans = max(ans, maxpos[it->first] - it->second);
    }
  }
  cout << ans << endl;
  return 0;
}
0