麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > Ruby > 正文

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

2020-10-29 19:39:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

復(fù)制代碼 代碼如下:

#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}"


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品毛片va一区二区三区 | 久久久久中文字幕 | 深夜福利久久久 | 免费a级网站 | 成人在线视频黄色 | aaaaa国产欧美一区二区 | 久久免费视频一区 | 日本中文字幕电影在线观看 | 欧美人一级淫片a免费播放 久久久久久久久91 国产99久久久久久免费看 | 色视频在线播放 | 久久99精品国产99久久6男男 | 亚洲精品久久久久www | 高颜值美女啪啪 | 久久精品视频亚洲 | 国产精品久久久久影院老司 | 欧美巨根 | 日本免费不卡一区二区 | 精品一区二区三区免费看 | aa国产视频一区二区 | 精品国产乱码久久久久久丨区2区 | 另类亚洲孕妇分娩网址 | 91精品国产91 | 嗯~啊~弄嗯~啊h高潮视频 | 4399一级成人毛片 | 手机视频在线播放 | 免费黄色小视频网站 | 一区二区久久久久草草 | 亚洲va在线| 91精品久久久久久久 | 精品国产一区二区亚洲人成毛片 | 91网址在线观看 | 99精彩视频在线观看 | 被啪羞羞视频在线观看 | 717影院理论午夜伦八戒秦先生 | 国产精品成人久久 | 精品一区二区三区中文字幕 | 国产午夜精品久久久久婷 | 99最新地址 | 国产精品视频一区二区三区四 | 久久久久久艹 | 成年性羞羞视频免费观看无限 |