寻找最长递增数字子串

    有一个字符串,其中包含了多个数字子串,要求求出最长的递增数字子串,比如,s='asdf1123as456df112365asdfa'中最长递增数字子串是11236.

string="asdfasdfasdf1111112222222356789462124asdfasdfasdfa9874563214756asdfasdfasdfasdfasdfasdfalkjlaskdjflkasdf124578"
s=string.scan(/\d+/)
#t=s[1].split //
#puts t.size
#print t,"\n"
ss=[]
hash={}
max=tt=0
s.each do |t|
    t=t.split //
for i in 0...(t.size)
    if(t[i+1]&&t[i+1]>=t[i])
         ss<<t[i]
        tt+=1
        if(tt>max)
            max=tt
            hash[ss.join('')]=max
        end
    else
        ss<<t[i]
        hash[ss.join('')]=ss.size
        ss=[]
        tt=0
    end
end
end
puts  hash.sort_by {|k,v|k.size}.last[0]
#puts hash




应该会有更简洁的方法的,慢慢再想!!!!

你可能感兴趣的:(正则表达式,Ruby,idea)