結果

問題 No.672 最長AB列
ユーザー SSRS
提出日時 2020-11-19 17:43:18
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 463 bytes
コンパイル時間 1,980 ms
コンパイル使用メモリ 176,904 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-07-23 10:08:34
合計ジャッジ時間 3,055 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  string S;
  cin >> S;
  int N = S.size();
  vector<int> s(N + 1);
  s[0] = 0;
  for (int i = 0; i < N; i++){
    if (S[i] == 'A'){
      s[i + 1] = s[i] + 1;
    } else {
      s[i + 1] = s[i] - 1;
    }
  }
  map<int, int> mp;
  int ans = 0;
  for (int i = 0; i <= N; i++){
    if (mp.count(s[i])){
      ans = max(ans, i - mp[s[i]]);
    } else {
      mp[s[i]] = i;
    }
  }
  cout << ans << endl;
}
0