結果

問題 No.672 最長AB列
ユーザー 0w10w1
提出日時 2018-04-13 22:34:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 724 bytes
コンパイル時間 2,365 ms
コンパイル使用メモリ 204,612 KB
最終ジャッジ日時 2025-01-05 10:03:36
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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