結果

問題 No.517 壊れたアクセサリー
ユーザー simansiman
提出日時 2021-11-05 05:17:06
言語 Ruby
(3.3.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,062 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-23 12:50:45
合計ジャッジ時間 2,770 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
12,288 KB
testcase_01 AC 79 ms
12,288 KB
testcase_02 AC 78 ms
12,416 KB
testcase_03 AC 77 ms
12,160 KB
testcase_04 AC 79 ms
12,288 KB
testcase_05 AC 79 ms
12,288 KB
testcase_06 AC 77 ms
12,160 KB
testcase_07 AC 77 ms
12,416 KB
testcase_08 AC 76 ms
12,416 KB
testcase_09 AC 78 ms
12,160 KB
testcase_10 AC 76 ms
12,160 KB
testcase_11 AC 79 ms
12,288 KB
testcase_12 AC 79 ms
12,160 KB
testcase_13 AC 79 ms
12,416 KB
testcase_14 AC 80 ms
12,416 KB
testcase_15 AC 79 ms
12,160 KB
testcase_16 AC 78 ms
12,416 KB
testcase_17 AC 80 ms
12,416 KB
testcase_18 AC 76 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:70: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

N = gets.to_i
A = N.times.map { gets.chomp }
M = gets.to_i
B = M.times.map { gets.chomp }
S = A.map(&:size).sum

E1 = Hash.new { |h, k| h[k] = Hash.new(false) }
ANS = []

N.times do |i|
  a1 = A[i].chars
  a1.each_cons(2) do |c1, c2|
    E1[c1][c2] = true
  end

  N.times do |j|
    next if i == j

    a2 = A[j].chars
    E1[a1.last][a2.first] = true
  end
end

E2 = Hash.new { |h, k| h[k] = Hash.new(false) }

M.times do |i|
  b1 = B[i].chars
  b1.each_cons(2) do |c1, c2|
    E2[c1][c2] = true
  end

  M.times do |j|
    next if i == j

    b2 = B[j].chars
    E2[b1.last][b2.first] = true
  end
end

def dfs(c, str, visited)
  return if ANS.size >= 2
  visited[c] = true

  if str.size == S
    ANS << str
    visited[c] = false
    return
  end

  E1[c].each do |nc, _|
    next if visited[nc]
    next if not E2[c][nc]

    dfs(nc, str + nc, visited)
  end

  visited[c] = false
end

A.each do |a|
  next if not B.find { |b| a[0] == b[0] }

  visited = Hash.new(false)
  dfs(a[0], a[0], visited)
end

if ANS.size == 1
  puts ANS.first
else
  puts -1
end
0