--自写算法代码
local p = function( t )
print(table.concat(t," "))
end
local clone = function(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index , value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table , getmetatable(object))
end
return _copy(object)
end
--interview at that year
print_order = function( list ,list_ret)
if #list <= 0 then
p(list_ret," ")
return
end
for _,v in ipairs(list) do
local list2 = clone(list)
local list2_ret = clone(list_ret)
if next(list2_ret) and next(list2) and list2_ret[#list2_ret] + list2[_] == 7 or list2_ret[3] and list2_ret[3] ==3 then
else
table.insert(list2_ret,table.remove(list2,_))
print_order(list2,list2_ret)
end
end
end
--quick
qsort = function( array,low,high )
if low>=high then return end
local _low,_high,key = low,high,array[low]
while(_low<_high)do
while(_low<_high and key<=array[_high])do _high = _high - 1 end
array[_low] = array[_high]
while(_low<_high and key>=array[_high])do _low = _low + 1 end
array[_high] = array[_low]
end
array[_low] = key
qsort(array,low,_low-1)
qsort(array,_low+1,high)
end
--kmp
local Next = function( ptr )
local _next = {0}
for i=2,#ptr do
local j = i
while(j > 1)do
j = _next[j-1] + 1
local _char1 = string.sub(ptr,i,i)
local _char2 = string.sub(ptr,j,j)
if _char2 == _char1 then
_next[i] = j
break
else
_next[i] = 0
end
end
end
return _next
end
local kmp = function( str , ptr)
local _next = Next(ptr)
local j = 1
for i=1,#str do
local _char1 = string.sub(str,i,i)
local _char2 = string.sub(ptr,j,j)
if _char1 == _char2 then
if j <#ptr then
j = j + 1
else
return i - #ptr + 1
end
else
if j > 1 then
j = _next[j-1] + 1
end
end
end
return nil
end
--p(Next("abcabcd"))
--print(tostring(kmp("baa","aa")))
--heapsort
heapadjust = function( array,ind,lenth )
local left = ind*2
if left<=lenth then
local key = array[ind]
if left+1 <= lenth and array[left+1] > array[left] then
left = left+1
end
if array[left] > key then
array[ind] = array[left]
array[left] = key
heapadjust(array,left,lenth)
end
end
end
heapsort = function( array ,lenth )
for i=math.floor(lenth/2),1,-1 do
heapadjust(array,i,lenth)
end
for i=lenth,2,-1 do
local key = array[i]
array[i] = array[1]
array[1] = key
heapadjust(array,1,i-1)
end
end
--ac mechine
local acNode = function( char,parent )
return {
char = char,
childs = {},
parent = parent,
mask = false,
fail = nil
}
end
local add = function(root, word )
local cur = root
for i=1,#word do
local _char1 = string.sub(word,i,i)
if not cur.childs[_char1] then
cur.childs[_char1] = acNode(_char1,cur)
end
cur = cur.childs[_char1]
end
cur.mask = word
end
local buildTrie = function( array )
local root = acNode()
for _,word in ipairs(array) do
add(root,word)
end
--------build failure------------
local queue = {}
table.insert(queue,root)
while(next(queue))do
local cur = table.remove(queue,1)
for k,node in pairs(cur.childs) do
table.insert(queue,node)
end
if cur.char then
if not cur.parent.char then
cur.fail = root
else
local fail = cur.parent.fail
while(fail and not fail.childs[cur.char]) do
fail = fail.fail
end
cur.fail = fail and fail.childs[cur.char] or root
end
end
end
return root
end
local acMatch = function( trie , ptr )
local cur = trie
local rec = {}
for i=1,#ptr do
local _char1 = string.sub(ptr,i,i)
while(true)do
if cur.childs[_char1] then
cur = cur.childs[_char1]
if cur.mask then
rec[i-#cur.mask+1] = cur.mask
end
break
elseif cur.fail then
cur = cur.fail
else
break
end
end
end
return rec
end
local spwanAc = function( array )
local trie = buildTrie(array)
return function( word )
local masks = acMatch(trie,word)
for start,mask in pairs(masks) do
word = string.gsub(word,mask,string.rep("*",#mask))
end
return word
end
end
local array = {"say","ty","mask","he","her","hs"}
local word = "yashetttrasmask"
local ac = spwanAc(array)
print(ac(word))