用LUA刷PAT (Advanced Level) ——1022 Digital Library

踩的坑

测试点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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值