好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Ruby實現(xiàn)的最短編輯距離計算方法

Ruby實現(xiàn)的最短編輯距離計算方法

熱門標簽:地圖標注賺錢項目注冊 徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個好 白銀外呼paas系統(tǒng) 高德地圖標注客服 湖州u友防封電銷卡 電銷機器人廠商代理 常德電銷平臺外呼系統(tǒng)軟件價格 百度地圖標注自定義圖片 滴滴外呼系統(tǒng)

利用動態(tài)規(guī)劃算法,實現(xiàn)最短編輯距離的計算。

復制代碼 代碼如下:

#encoding: utf-8
#author: xu jin
#date: Nov 12, 2012
#EditDistance
#to find the minimum cost by using EditDistance algorithm
#example output:
#  "Please input a string: "
#  exponential
#  "Please input the other string: "
#  polynomial
#  "The expected cost is 6"
#  The result is :
#    ["e", "x", "p", "o", "n", "e", "n", "-", "t", "i", "a", "l"]
#    ["-", "-", "p", "o", "l", "y", "n", "o", "m", "i", "a", "l"]

p "Please input a string: "
x = gets.chop.chars.map{|c| c}
p "Please input the other string: "
y = gets.chop.chars.map{|c| c}
x.unshift(" ")
y.unshift(" ")
e = Array.new(x.size){Array.new(y.size)}
flag = Array.new(x.size){Array.new(y.size)}
DEL, INS, CHA, FIT = (1..4).to_a  #deleat, insert, change, and fit
 
def edit_distance(x, y, e, flag)
  (0..x.length - 1).each{|i| e[i][0] = i}
  (0..y.length - 1).each{|j| e[0][j] = j}
  diff = Array.new(x.size){Array.new(y.size)}
  for i in(1..x.length - 1) do
    for j in(1..y.length - 1) do
      diff[i][j] = (x[i] == y[j])? 0: 1
      e[i][j] = [e[i-1][j] + 1, e[i][j - 1] + 1, e[i-1][j - 1] + diff[i][j]].min
      if e[i][j] == e[i-1][j] + 1
        flag[i][j] = DEL
      elsif e[i][j] == e[i-1][j - 1] + 1
        flag[i][j] = CHA
      elsif e[i][j] == e[i][j - 1] + 1
        flag[i][j] = INS      
      else flag[i][j] = FIT
      end    
    end
  end 
end

out_x, out_y = [], []

def solution_structure(x, y, flag, i, j, out_x, out_y)
  case flag[i][j]
  when FIT
    out_x.unshift(x[i])
    out_y.unshift(y[j]) 
    solution_structure(x, y, flag, i - 1, j - 1, out_x, out_y)
  when DEL
    out_x.unshift(x[i])
    out_y.unshift('-')
    solution_structure(x, y, flag, i - 1, j, out_x, out_y)
  when INS
    out_x.unshift('-')
    out_y.unshift(y[j])
    solution_structure(x, y, flag, i, j - 1, out_x, out_y)
  when CHA
    out_x.unshift(x[i])
    out_y.unshift(y[j])
    solution_structure(x, y, flag, i - 1, j - 1, out_x, out_y)
  end
  #if flag[i][j] == nil ,go here
  return if i == 0 j == 0   
  if j == 0
      out_y.unshift('-')
      out_x.unshift(x[i])
      solution_structure(x, y, flag, i - 1, j, out_x, out_y)
  elsif i == 0
      out_x.unshift('-')
      out_y.unshift(y[j])
      solution_structure(x, y, flag, i, j - 1, out_x, out_y)
  end
end

edit_distance(x, y, e, flag)
p "The expected edit distance is #{e[x.length - 1][y.length - 1]}"
solution_structure(x, y, flag, x.length - 1, y.length - 1, out_x, out_y)
puts "The result is : \n  #{out_x}\n  #{out_y}"


您可能感興趣的文章:
  • Ruby實現(xiàn)的各種排序算法
  • Ruby實現(xiàn)的合并排序算法
  • Ruby實現(xiàn)的3種快速排序算法
  • Ruby一行代碼實現(xiàn)的快速排序
  • ruby實現(xiàn)的插入排序和冒泡排序算法
  • Ruby實現(xiàn)的最長公共子序列算法
  • Ruby實現(xiàn)的最優(yōu)二叉查找樹算法
  • Ruby最簡單的消息服務(wù)器代碼
  • Ruby的字符串與數(shù)組求最大值的相關(guān)問題討論

標簽:三沙 張家界 遼寧 公主嶺 永州 荊門 普洱 梧州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Ruby實現(xiàn)的最短編輯距離計算方法》,本文關(guān)鍵詞  Ruby,實現(xiàn),的,最短,編輯,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby實現(xiàn)的最短編輯距離計算方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于Ruby實現(xiàn)的最短編輯距離計算方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 18禁无遮挡啪啪久久久免费观看| 国产欧美日韩在线观看无需安装| 国产激情电影综合在线看免费| 第七色导航| 被男人吃奶| 秋霞免费一级鲁丝片| 校园寝室双性受h| 蜜瓜影院| 女同AV国产女同精品99| 和黑道大佬的365天| 99久久99这里只有免费费精品| 羞羞3d动漫视频在线观看| 媚妇放荡小说| 啊啊啊不要舔| 蜜臀久久99精品久久久久久酒店| 稀缺呦精品呦视频www| 色老板在线免费| 女教师被校长乱淫| 玉米地玉民初试云雨情| 中文韩国午夜理伦三级好看| jizzxxx4hd| 姨母的诱惑| 亚洲欧美日韩系列| 麻麻张开腿让我爽了一夜视频| 三级在线免费看| 国产精品亚洲欧美日韩久久| 影音先锋ady69色资源网站| 四虎成人精品视频在线观看| 小男生体检h系列小说bl| 男女一级大黄| 美女又爽又黄免费| 一级a一级a爰片免费免免软件ww | 禁漫?????????动漫精灵| 亚洲一区二区毛片| 被和尚摧残蹂躏| 美妇排撅着光溜溜屁股| 老板和我共享娇妻| 国产精品高潮呻吟Ⅴa无码网曝门 久久久久久精品免费免费看片 | 激情综合色综合啪啪开心| 翘臀美女后进啪图| 一级特黄特色的免费大片视频|