結果

問題 No.517 壊れたアクセサリー
ユーザー letrangerjpletrangerjp
提出日時 2017-05-28 23:01:03
言語 Ruby
(3.3.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 54 ms
コンパイル使用メモリ 11,948 KB
実行使用メモリ 16,124 KB
最終ジャッジ日時 2023-10-21 14:31:34
合計ジャッジ時間 2,034 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
16,124 KB
testcase_01 AC 74 ms
16,124 KB
testcase_02 AC 68 ms
16,124 KB
testcase_03 AC 68 ms
16,124 KB
testcase_04 AC 68 ms
16,124 KB
testcase_05 AC 67 ms
16,124 KB
testcase_06 AC 68 ms
16,124 KB
testcase_07 AC 67 ms
16,124 KB
testcase_08 AC 71 ms
16,124 KB
testcase_09 AC 67 ms
16,120 KB
testcase_10 AC 67 ms
16,120 KB
testcase_11 AC 66 ms
16,124 KB
testcase_12 AC 67 ms
16,124 KB
testcase_13 AC 69 ms
16,124 KB
testcase_14 AC 70 ms
16,120 KB
testcase_15 AC 70 ms
16,124 KB
testcase_16 AC 68 ms
16,124 KB
testcase_17 AC 68 ms
16,124 KB
testcase_18 AC 66 ms
16,124 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def f(a, b)
  r = nil
  found = false
  a.each_with_index{|aa, i|
    used_a = [false] * A.size
    used_b = [false] * B.size

    g = lambda{|a, b|
      # p [a, b]
      # p [used_a, used_b]
      if used_a.all?{|v|v} && used_b.all?{|v|v}
        return a == b && a
      end
      if a.size > b.size
        B.each_with_index{|bb, i|
          next if used_b[i]
          new_b = b + bb
          len = [new_b.size, a.size].min
          if new_b[0...len] == a[0...len]
            used_b[i] = true
            if rr = g.(a, new_b)
              return rr
            end
            used_b[i] = false
          end
        }
      else
        A.each_with_index{|aa, i|
          next if used_a[i]
          new_a = a + aa
          len = [new_a.size, b.size].min
          if new_a[0...len] == b[0...len]
            used_a[i] = true
            if rr = g.(new_a, b)
              return rr
            end
            used_a[i] = false
          end
        }
      end
      false
    }
    used_a[i] = true
    if tmp = g.(aa, "")
      return false if found
      r = tmp
      found = true
    end
  }
  r
end

N = gets.to_i
A = N.times.map{gets.chomp}
M = gets.to_i
B = M.times.map{gets.chomp}

r = f(A, B)
puts r ? r : -1
0