結果

問題 No.672 最長AB列
ユーザー DaYuanChiDaYuanChi
提出日時 2021-01-27 01:04:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 570 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 171,472 KB
実行使用メモリ 15,156 KB
最終ジャッジ日時 2023-09-06 04:05:14
合計ジャッジ時間 3,398 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 90 ms
15,156 KB
testcase_10 AC 66 ms
10,528 KB
testcase_11 AC 60 ms
10,332 KB
testcase_12 AC 18 ms
5,844 KB
testcase_13 AC 18 ms
5,780 KB
testcase_14 AC 19 ms
5,840 KB
testcase_15 AC 18 ms
5,768 KB
testcase_16 AC 18 ms
5,768 KB
testcase_17 AC 62 ms
10,460 KB
testcase_18 AC 9 ms
5,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int a[200005];
int b[200005];
int c[200005];
map<int,int> mp;

int main(){
  string s;
  cin >> s;
  int n = s.size();
  for(int i = 1; i <= n; i++){
    if(s[i-1]=='A'){
      a[i] = a[i-1]+1;
      b[i] = b[i-1];
    }else{
      a[i] = a[i-1];
      b[i] = b[i-1]+1;
    }
  }
  int ans = 0;
  for(int i = 1; i <= n; i++) c[i] = a[i]-b[i];
  for(int i = 0; i <= n; i++){
    if(mp.find(c[i])==mp.end()) mp[c[i]] = i;
    else ans = max(ans, i-mp[c[i]]);
  }
  cout << ans << endl;    
  return 0;
}
0