結果
| 問題 | No.672 最長AB列 | 
| コンテスト | |
| ユーザー | 👑 | 
| 提出日時 | 2020-03-07 20:31:09 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 49 ms / 2,000 ms | 
| コード長 | 822 bytes | 
| コンパイル時間 | 690 ms | 
| コンパイル使用メモリ | 76,152 KB | 
| 実行使用メモリ | 6,680 KB | 
| 最終ジャッジ日時 | 2024-10-15 14:28:27 | 
| 合計ジャッジ時間 | 1,647 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 16 | 
ソースコード
#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;
  
}
            
            
            
        