結果

問題 No.672 最長AB列
ユーザー ikd
提出日時 2018-04-14 19:35:11
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 773 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 94,756 KB
実行使用メモリ 35,168 KB
最終ジャッジ日時 2024-06-13 00:26:31
合計ジャッジ時間 1,964 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  auto s=readln.chomp.to!(char[]);

  int[int] posl, posr;
  int n=s.length.to!(int), na=0, nb=0;
  posl[na-nb]=posr[na-nb]=0;
  foreach(int i; 0..n){
    if(s[i]=='A') na++;
    if(s[i]=='B') nb++;
    auto dif=na-nb;
    if(dif in posl) chmin(posl[dif], i+1);
    else posl[dif]=i+1;
    if(dif in posr) chmax(posr[dif], i+1);
    else posr[dif]=i+1;
  }

  int len=0;
  foreach(k; posl.keys){
    chmax(len, posr[k]-posl[k]);
  }
  writeln(len);
}

void chmin(T)(ref T l, T r){if(l>r)l=r;}
void chmax(T)(ref T l, T r){if(l<r)l=r;}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
0