串S 串P 然后构建Next数组 代表退回的位置。匹配失败 匹配串就往Next[j-1]+1位置从新开始匹配  如果都回到1了 那就可以i++了 

lua下标为1开始 和C/C++不一样。这俩都是下标0开始的

S={4,3,4,41,46,5}
P={3,4,41,46,5} --这里可以试试{3,5,41,46,5} 字符串。可以改的
Next={}
function makeNext()
Next[1]=0;
for i=2,5,1 do
local j=Next[i-1]
while(S[i]~=P[j+1] and j>=1)do
j=Next[j]
end
if(S[i]==P[j+1])then
Next[i]=j+1
else
Next[i]=1
end
end
end
function KMP()
makeNext()
for now=1,6,1 do
local i=now
local pos=1
while(pos<5 and i<6)do
if(S[i]==P[pos])then
i=i+1
pos=pos+1
else
if(pos==1) then i=i+1
else pos=Next[pos-1]+1
end
end
end
if(pos==5 and (i-now)==5)then
print("P is sub")
return
end
end
print("not sub")
end
KMP()