LIS:最长非降子序列长度

import sys
import re
n=int (raw_input())
l=list(input())
print l
动态规划思想 (O(N^2)) refer:http://www.360doc.com/content/13/0601/00/8076359_289597587.shtml
'''le=1
d=[1]*n
for i in range(n):
    for j in range(i):#note before i sequence
        if l[j]<=l[i] and d[j]+1>d[i]:
            d[i]=d[j]+1
        if d[i]>le:
            le=d[i]
        j+=1
print le,d''' 

插入的思想,挺有心意的  refer:  https://www.felix021.com/blog/read.php?1587
def bise(l,start,end,key):
    if l[end]<=key:
        #l.append(key)
        return end+1
    while(start<end):
        mid=start+(end-start)/2
        if l[mid]<=key:
            start=mid+1
        else:
            end=mid
    return start def lis(d,n):
    lth=0 #
    res=[0]*n  res[0]=d[0]
    for i in range(1,n):
        pos=bise(res,0,lth,d[i])#insert l[i]
        print pos
        res[pos]=d[i]
        if lth<pos:
            lth=pos
    return lth,res

print lis(l,n) #just return correct length,no string
点赞