当前位置:网站首页>L1-023 output gplt (Lua)

L1-023 output gplt (Lua)

2022-07-07 19:13:00 Just be interesting

subject

A given length does not exceed 10000 Of 、 A string consisting only of English letters . Please reorder the characters , Press GPLTGPLT… Output in this order , And ignore the other characters . Of course , Four types of characters ( Case insensitive ) It doesn't have to be the same number , If a character has been printed out , Then press the remaining characters GPLT Sequential printing , Until all characters are printed .

Input format :
The input is given in a line with a length not exceeding 10000 Of 、 A non - empty string consisting only of English letters .

Output format :
Output the sorted string on a line by the title . The question guarantees that the output is not empty .

sample input :
pcTclnGloRgLrtLhgljkLhGFauPewSKgt

sample output :
GPLTGPLTGLTGLGLL

Code

local map = {
    }
local str = io.read()
str = str:upper() -- Capitalize 

-- initialization ,  Prevent uninitialized , That is to say nil
map["G"], map["P"], map["L"], map["T"] = 0, 0, 0, 0
for i = 1, #str do
    local ch = str:sub(i, i)
    if ch == "G" or ch == "P" or ch == "L" or ch == "T" then
        map[ch] = map[ch] + 1
    end
end

local gplt = {
     "G", "P", "L", "T"} -- Output order 
local sum = map["G"] + map["P"] + map["L"] + map["T"]
local s = "" -- Result string 
while sum > 0 do
    for i = 1, 4 do
        if map[gplt[i]] > 0 then
            sum = sum - 1
            s = s .. gplt[i]
            map[gplt[i]] = map[gplt[i]] - 1
        end
    end
end

print(s)
原网站

版权声明
本文为[Just be interesting]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071515233649.html