結果
| 問題 |
No.517 壊れたアクセサリー
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2022-02-18 00:02:51 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,500 bytes |
| コンパイル時間 | 50 ms |
| コンパイル使用メモリ | 6,692 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-29 07:54:41 |
| 合計ジャッジ時間 | 801 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 |
ソースコード
local n = io.read("*n", "*l")
local t = {}
for i = 1, 26 do t[i] = 0 end
local s1 = {}
for i = 1, n do
local s = io.read()
s1[i] = s
for j = 1, #s do
local v = s:byte(j) - 64
t[v] = t[v] + 1
end
end
local m = io.read("*n", "*l")
for i = 1, m do
local s = io.read()
s1[i + n] = s
for j = 1, #s do
local v = s:byte(j) - 64
t[v] = t[v] + 1
end
end
local w = {}
for i = 1, 26 do
if t[i] == 1 then print(-1) os.exit() end
if t[i] == 2 then
table.insert(w, i)
t[i] = #w
end
end
local parent = {}
local left, right = {}, {}
for i = 1, #w do
parent[i] = i
left[i], right[i] = false, false
end
local function uf_findroot(idx)
local idx_update = idx
while parent[idx] ~= idx do
idx = parent[idx]
end
while parent[idx_update] ~= idx do
parent[idx_update], idx_update = idx, parent[idx_update]
end
return idx
end
for i = 1, n + m do
local si = s1[i]
for j = 1, #si - 1 do
local z1, z2 = t[si:byte(j) - 64], t[si:byte(j + 1) - 64]
local r1, r2 = uf_findroot(z1), uf_findroot(z2)
if r1 ~= r2 then
parent[r2], parent[z2] = r1, r1
end
if right[z1] and right[z1] ~= z2 then
print(-1) os.exit()
end
if left[z2] and left[z2] ~= z1 then
print(-1) os.exit()
end
left[z2] = z1
right[z1] = z2
end
end
local r = uf_findroot(1)
for i = 2, #w do
if r ~= uf_findroot(i) then
print(-1) os.exit()
end
end
while r do
io.write(string.char(64 + w[r]))
r = right[r]
end
io.write("\n")