Ruby實(shí)現(xiàn)的最短編輯距離計(jì)算方法
利用動(dòng)態(tài)規(guī)劃算法,實(shí)現(xiàn)最短編輯距離的計(jì)算。
#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}"
相關(guān)文章
關(guān)于Ruby on Rails視圖編寫(xiě)的一些建議
這篇文章主要介紹了關(guān)于Ruby on Rails視圖編寫(xiě)的一些建議,有助于團(tuán)隊(duì)協(xié)作時(shí)對(duì)代碼的調(diào)試工作,需要的朋友可以參考下2015-08-08進(jìn)一步深入Ruby中的類(lèi)與對(duì)象概念
這篇文章主要介紹了進(jìn)一步深入Ruby中的類(lèi)與對(duì)象概念,包括集成與多態(tài)等更多知識(shí)點(diǎn)的整理,需要的朋友可以參考下2015-05-05Ruby 中的 module_function 和 extend self異同
本文主要給大家介紹了在Ruby中 module_function 和 extend self的共同點(diǎn)和區(qū)別,非常的詳細(xì),也很實(shí)用,方便大家更好的理解的module_function 和 extend self2017-05-05使用Ruby來(lái)編寫(xiě)訪問(wèn)Twitter的命令行應(yīng)用程序的教程
這篇文章主要介紹了使用Ruby來(lái)編寫(xiě)訪問(wèn)Twitter的命令行應(yīng)用程序的教程,文章來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04Rails中使用MySQL分區(qū)表一個(gè)提升性能的方法
這篇文章主要介紹了Rails中使用MySQL分區(qū)表一個(gè)提升性能的方法,本文總結(jié)出了一個(gè)簡(jiǎn)單的方法實(shí)現(xiàn)避免掃描全部的分區(qū)表,從而提升性能,需要的朋友可以參考下2015-03-03詳解Ruby設(shè)計(jì)模式編程中對(duì)單例模式的運(yùn)用
這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中對(duì)單例模式的運(yùn)用,講到了包括對(duì)Singleton模塊的使用,需要的朋友可以參考下2016-03-03