結果

問題 No.672 最長AB列
ユーザー aim_cpo
提出日時 2018-04-13 23:18:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,221 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 166,864 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 16:47:59
合計ジャッジ時間 2,353 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

string s;
int n;
int acont[200001],bcont[200001];

bool check(int mid){
  if(mid&1)return 1;
  for(int i=0;i+mid<=n;i++){
    if(i==0){
      if(acont[i+mid-1]==bcont[i+mid-1])return 1;
      continue;
    }
    if(acont[i+mid]-acont[i-1]==bcont[i+mid]-bcont[i-1])return 1;
  }
  return 0;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout<<fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  //FILE *stream2;
  stream1=freopen("in","r",stdin);
  //stream2=freopen("out","w",stdout);
  if(stream1==NULL)return 0;
  //if(stream2==NULL)return 0;
#endif
  cin>>s;
  n=(int)s.size();
  for(int i=0;i<n;i++){
    if(i==0){
      if(s[i]=='A')acont[i]=1;
      else bcont[i]=1;
    }
    if(s[i]=='A'){acont[i]=acont[i-1]+1;bcont[i]=bcont[i-1];}
    else {bcont[i]=bcont[i-1]+1;acont[i]=acont[i-1];}
  }
  int ok=0,ng=n+1;
  while(ng-ok>1){
    int mid=(ok+ng)/2;
    if(check(mid)){
      ok=mid;
    }else{
      ng=mid;
    }
  }
  if(ok&1)cout<<ok-1<<endl;
  else cout<<ok<<endl;
#ifdef LOCAL_DEFINE
  cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
  fclose(stream1);
  //fclose(stream2);
#endif
  return 0;
}
0