結果

問題 No.672 最長AB列
ユーザー urutomurutom
提出日時 2019-04-03 21:06:15
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 580 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 11,280 KB
実行使用メモリ 19,312 KB
最終ジャッジ日時 2023-08-24 21:10:29
合計ジャッジ時間 3,412 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,048 KB
testcase_01 AC 83 ms
15,112 KB
testcase_02 AC 85 ms
15,248 KB
testcase_03 AC 89 ms
15,164 KB
testcase_04 AC 83 ms
15,132 KB
testcase_05 AC 87 ms
15,048 KB
testcase_06 AC 86 ms
15,276 KB
testcase_07 AC 86 ms
15,252 KB
testcase_08 WA -
testcase_09 AC 83 ms
15,408 KB
testcase_10 AC 83 ms
15,396 KB
testcase_11 AC 83 ms
15,360 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 85 ms
15,432 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=
    a_num<b_num ? [?A,?B] : [?B,?A]
min_char_pos=[*0...n].select{|i|s[i]==min_char}
until min_char_pos.empty?
  size=min_char_pos.size
  if size==1
    puts 2
    exit
  end
  d=min_char_pos.last-min_char_pos.first
  if d>size*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 size*2
    exit
  end
end
0