結果

問題 No.672 最長AB列
ユーザー CleyLCleyL
提出日時 2020-03-07 20:31:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 76,480 KB
実行使用メモリ 6,936 KB
最終ジャッジ日時 2024-04-23 14:19:06
合計ジャッジ時間 1,962 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 23 ms
6,676 KB
testcase_10 AC 49 ms
6,680 KB
testcase_11 AC 55 ms
6,804 KB
testcase_12 AC 35 ms
6,800 KB
testcase_13 AC 36 ms
6,932 KB
testcase_14 AC 34 ms
6,808 KB
testcase_15 AC 33 ms
6,800 KB
testcase_16 AC 34 ms
6,936 KB
testcase_17 AC 26 ms
6,808 KB
testcase_18 AC 26 ms
6,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct dta{
  int n;
  int A,B;
};
bool cmp(const dta &a,dta &b){
  if(a.A==b.A){
    if(a.B==b.B){
      return a.n < b.n;
    }
    return a.B < b.B;
  }
  return a.A < b.A;

}
int nwa,nwb;
int main(){
  string s;cin>>s;
  vector<dta> A;
  A.push_back({-1,0,0});
  for(int i = 0; s.size() > i; i++){
    if(s[i] == 'A')nwa++;
    else nwb++;
    if(nwa*nwb){
      nwa--;
      nwb--;
    }
    A.push_back({i,nwa,nwb});
  }
  sort(A.begin(),A.end(),cmp);
  int nA = A[0].A;
  int nB = A[0].B;
  int nN = A[0].n;
  int ans = 0;
  for(int i = 1; A.size() > i; i++){
    if(nA == A[i].A && nB == A[i].B){
      ans = max(ans,A[i].n-nN);
    }else{
      nA = A[i].A;
      nB = A[i].B;
      nN = A[i].n;
    }
  }
  cout << ans << endl;
  
}
0