【由留学作业帮www.homeworkhelp.cc整理编辑】The input to this problem is a pair of strings

The input to this problem is a pair of strings A = a1 · · · am and B = b1 · · · bn. The goal is to convert A into B as cheaply as possible. The rules and cost are defined as follow. For a cost of 3 you can delete any letter. For a cost of 4 you can insert a letter in any position. For a cost of 5 you can replace any letter by any other letter. For example, you can convert A = abcabc to B = abacab via the following sequence: abcabc at a cost of 5 can be converted to abaabc, which at cost of 3 can be converted to ababc, which at cost of 3 can be converted to abac, which at cost of 4 can be converted to abacb, which at cost of 4 can be converted to abacab. Thus the total cost for this conversion would be 19. This is probably not the cheapest possible conversion. (Note that the Unix diff command essentially solves this problem.)

(a) Give a dynamic programming algorithm to solve this problem.

(b) Apply your algorithm in (a) to convert A = abcabc to B = abacab with the minimum total cost. Show all your work
ANSWER:

Construct a table T where each entry T[i, j] = cxy(i, j). Since each value

of cxy(i, j) only depends on cxy(i , j� )

由留学作业帮www.homeworkhelp.cc整理编辑

where i � i and j� � j, we can compute the entries of T row by row
EDIT-DISTANCE (x[1…m], y[1…n]) 1 T[0, 0] � 0 2 for i � 1 to n 3 for j � 1 to m ⎧ T[i − 1, j − 1] if i > 0 and j > 0 and x[i − 1] = y[j − 1] ⎧� T[i − 1, j − 1] + 4 if i > 0 and j > 0 4 do T[i, j] � min ⎧⎧ T[i − 1, j] + 2 if i > 0 T[i, j − 1] + 3 if j > 0 5

return T[n, m]

The running time of this algorithm is �(mn). This algorithm requires �(mn) space.

SORRY I ONLY SURE ABOUT FIRST ONE THAT’S WHY I DO THAT SORRY

IF U NEED ANY HELP PLZ LEAVE A COMMENT !!

AND PLZZ DO THUMBS UP IT’S REALLY URGENT. THANK YOU

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值