LIS:最长非降子序列长度

来源:互联网 发布:潘安传奇网络电影 编辑:程序博客网 时间:2024/06/06 09:57
import sysimport ren=int (raw_input())l=list(input())print l
动态规划思想 (O(N^2)) refer:http://www.360doc.com/content/13/0601/00/8076359_289597587.shtml'''le=1d=[1]*nfor 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+=1print le,d''' 插入的思想,挺有心意的  refer:  https://www.felix021.com/blog/read.php?1587def 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 startdef 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,resprint lis(l,n) #just return correct length,no string