踩的坑
测试点3:id可能开头为0,不可直接当成数字处理
测试点4:keyword可能有字符和数字之外的东西(我猜可能有连词符什么的)
整体还是相当简单的,对性能要求也不高,这里每个id至少存了五次都没事
代码
--paras
local title2id = {}
local author2id = {}
local key2id = {}
local publisher2id = {}
local year2id = {}
local getId = {title2id, author2id, key2id, publisher2id, year2id}
--logic
function insert(key, value, t)
if not t[key] then
t[key] = {}
end
table.insert(t[key], value)
end
--main
local N = tonumber(io.read())
for _ = 1, N do
local id = io.read()
insert(io.read(), id, title2id)
insert(io.read(), id, author2id)
for key in io.read():gmatch('[%p*%w%p*]+') do
insert(key, id, key2id)
end
insert(io.read(), id, publisher2id)
insert(io.read(), id, year2id)
end
N = tonumber(io.read())
for _ = 1, N do
local input = io.read()
print(input)
t = tonumber(string.sub(input, 1, 1))
v = string.sub(input, 4)
local result_array = getId[tonumber(t)][v]
if not result_array then
print("Not Found")
else
table.sort(result_array, function (a, b) return tonumber(a) < tonumber(b) end)
for _, id in pairs(result_array) do
print(id)
end
end
end