欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python基于動態(tài)規(guī)劃算法計算單詞距離

 更新時間:2015年07月25日 14:44:55   作者:Sephiroth  
這篇文章主要介紹了Python基于動態(tài)規(guī)劃算法計算單詞距離的方法,實例分析了Python動態(tài)規(guī)劃算法的實現(xiàn)與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python基于動態(tài)規(guī)劃算法計算單詞距離。分享給大家供大家參考。具體如下:

#!/usr/bin/env python
#coding=utf-8
def word_distance(m,n):
  """compute the least steps number to convert m to n by insert , delete , replace .
  動態(tài)規(guī)劃算法,計算單詞距離
  >>> print word_distance("abc","abec")
  1
  >>> print word_distance("ababec","abc")
  3
  """
  len_1=lambda x:len(x)+1
  c=[[i] for i in range(0,len_1(m)) ]
  c[0]=[j for j in range(0,len_1(n))]
  for i in range(0,len(m)):
  #  print i,' ',
    for j in range(0,len(n)):
      c[i+1].append(
        min(
          c[i][j+1]+1,#插入n[j]
          c[i+1][j]+1,#刪除m[j]
          c[i][j] + (0 if m[i]==n[j] else 1 )#改
        )
      )
  #    print c[i+1][j+1],m[i],n[j],' ',
  #  print ''
  return c[-1][-1]
import doctest
doctest.testmod()
raw_input("Success!")

希望本文所述對大家的Python程序設計有所幫助。

相關文章

最新評論