結果

問題 No.672 最長AB列
ユーザー urutomurutom
提出日時 2019-04-03 21:03:21
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 609 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 11,236 KB
実行使用メモリ 19,300 KB
最終ジャッジ日時 2023-08-24 21:07:58
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,252 KB
testcase_01 AC 79 ms
15,224 KB
testcase_02 AC 77 ms
15,072 KB
testcase_03 AC 79 ms
15,132 KB
testcase_04 AC 78 ms
15,080 KB
testcase_05 AC 79 ms
15,120 KB
testcase_06 AC 78 ms
15,160 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 79 ms
15,440 KB
testcase_10 AC 79 ms
15,356 KB
testcase_11 AC 79 ms
15,576 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 78 ms
15,456 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:14: warning: assigned but unused variable - max_char
Syntax OK

ソースコード

diff #

s=gets.chomp
n=s.size
a_num=s.count(?A)
b_num=n-a_num
if a_num==b_num
  puts n
  exit
end
if a_num==0||b_num==0
  puts 0
  exit
end

min_char,max_char,min_char_num=
    a_num<b_num ? [?A,?B,a_num] : [?B,?A,b_num]
min_char_pos=[*0...n].select{|i|s[i]==min_char}
until min_char_pos.empty?
  if min_char_pos.size==1
    puts 2
    exit
  end
  d=min_char_pos.last-min_char_pos.first
  if d>min_char_num*2
    d1=min_char_pos[1]-min_char_pos[0]
    d2=min_char_pos[-1]-min_char_pos[-2]
    if d1>d2
      min_char_pos.shift
    else
      min_char_pos.pop
    end
  else
    puts min_char_num*2
    exit
  end
end
0