问题描述
动态规划求解
伪代码
插入删除均为-3,match为+1,dismatch为-1
python实现
# -*- coding:utf-8 -*-
# 动态规划-序列连配问题:编辑距离
import numpy as np
def Needleman_Wunch(S, T):
opt = np.zeros((len(T)+1, len(S)+1))
for i in range(len(S)+1):
opt[0][i] = -3 * i
for j in range(len(T)+1):
opt[j][0] = -3 * j
for i in range(1, len(T)+1):
for j in range(1, len(S)+1):
if T[i-1]==S[j-1]:
x = 1
else:
x = -1
opt[i][j] = max([opt[i-1][j-1] + x, opt[i][j-1] -3, opt[i-1][j]-3])
print(opt)
return opt[len(T), len(S)]
S = 'OCURRANCE'
T = 'OCCURRENCE'
print(Needleman_Wunch(S, T))