最长回文子串的解



    local inputStr = "casdfafdsab"
    local LENGTH = string.len(tostring(inputStr))

    local function createMaskTable(pTotalBitCount, pLength)
        local ret = {}
        for i = 1, pTotalBitCount do
            ret[i] = i > pLength and 0 or 1
        end
        return ret
    end

    local function rightMove(pTable)
        table.insert(pTable, 1, table.remove(pTable, #pTable))
        return pTable
    end

    local function check(str)
        print(str)
        local str = tostring(str)
        local len = string.len(str)
        local middle = math.ceil(len / 2)
        local isVailed = true
        for i = 1, middle do
            local left = string.sub(str, i, i)
            local right = string.sub(str, len - (i - 1), len - (i - 1))
            print("left Right : ", left, right)
            if left ~= right then
                isVailed = false
                break
            end
        end
        if isVailed then print(str.." ==== IsVaild") end
        return isVailed
    end

    for i = LENGTH, 1, -1 do
        local tab = createMaskTable(LENGTH, i)
        local str = ""
        for i = 1, #tab do
            if tab[i] == 1 then str = str..string.sub(inputStr, i, i) end
        end
        if check(str) then break end
        for k = 1, LENGTH - i do
            rightMove(tab, k)
            str = ""
            for i = 1, #tab do
                if tab[i] == 1 then str = str..string.sub(inputStr, i, i) end
            end
            if check(str) then return end
        end
    end


    do return end


版权声明:本文为qq_29094161原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。