結果

問題 No.205 マージして辞書順最小
ユーザー ich59669ich59669
提出日時 2016-02-03 09:33:19
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,127 ms / 5,000 ms
コード長 1,856 bytes
コンパイル時間 39 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 20,120 KB
最終ジャッジ日時 2023-10-21 18:48:38
合計ジャッジ時間 5,046 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
16,084 KB
testcase_01 AC 85 ms
16,084 KB
testcase_02 AC 103 ms
16,084 KB
testcase_03 AC 115 ms
16,608 KB
testcase_04 AC 105 ms
16,084 KB
testcase_05 AC 104 ms
16,136 KB
testcase_06 AC 85 ms
16,084 KB
testcase_07 AC 157 ms
17,012 KB
testcase_08 AC 126 ms
16,208 KB
testcase_09 AC 141 ms
16,556 KB
testcase_10 AC 1,127 ms
20,120 KB
testcase_11 AC 725 ms
19,416 KB
testcase_12 AC 473 ms
18,300 KB
testcase_13 AC 225 ms
17,444 KB
testcase_14 AC 85 ms
16,084 KB
testcase_15 AC 82 ms
16,084 KB
testcase_16 AC 84 ms
16,084 KB
testcase_17 AC 85 ms
16,084 KB
testcase_18 AC 85 ms
16,084 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def push_char_index(chrs, indexes, depth = 0)
  # 最小の文字を調べる
  minchar = "{"
  indexes.each do |index|
    if chrs[index].length > depth && chrs[index][depth] < minchar
      minchar = chrs[index][depth]
    end
  end
  # 最小の文字の位置を調べる
  minchar_indexes = []
  indexes.each do |index|
    if chrs[index].length > depth && chrs[index][depth] == minchar
      minchar_indexes << index
    end
  end
  # 最小の文字が1つしかないならその文字のindexを返す
  # 2つ以上ある場合で、次の文字があるならばその文字で調べる
  # indexes内のすべてのindexについて次の文字が存在しない場合、indexes[0]を返す
  if minchar_indexes.length == 1
    return minchar_indexes[0]
  elsif minchar_indexes.length == 0
    return indexes[0]
  else
    return push_char_index(chrs, minchar_indexes, depth + 1)
  end
end

def any_char_is(chrs, j_char)
  chrs.each do |char|
    return false if char[0] != j_char
  end
  return true
end

# トップレベルの開始
input_count = gets.to_i

# strs配列に文字列を格納
strs = []
input_count.times do
  strs << gets.chomp!
end

# 文字列を分割した配列を配列chrsに格納
chrs = []
strs.each do |str|
  chrs << str.split("")
end

# メソッドに渡すindexes配列の作成 ([0, 1, 2, ... ])
indexes = (0...chrs.length).collect{ |i| i}

sort = ""
# chrsが空になるまで
until any_char_is(chrs, "~") do
  
  # 追加すべき文字のインデックスを取得
  index = push_char_index(chrs, indexes)
  # 出力文字列sortに、追加すべき文字を挿入
  sort.concat(chrs[index][0])
  
  # 追加した文字をchrs[index]から削除
  chrs[index].shift
  
  # chrs[index]が空になったら番兵として~を追加
  if chrs[index].length == 0
    chrs[index] << "~"
  end
end
puts sort
0