ImageVerifierCode 换一换
格式:DOCX , 页数:11 ,大小:19.98KB ,
资源ID:16229425      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16229425.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(算法导论上机报告Word文档下载推荐.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

算法导论上机报告Word文档下载推荐.docx

1、that produces the same result at lower cost. If a character is replaced by a character aand then replaced again by a character b, then both operations can be replaced by “replace b”. This means that all inserted and replaced characters are never changed afterperforming the insertion or replacement.

2、Notice that after removing these operationsthat introduce dependencies, any sequence of insert, delete, and replace operations canbe reordered so that they occur from left to right without affecting the outcome of thetransformation.(c)We show that computing edit distance for strings x and y can be d

3、one by finding theedit distance of subproblems. Define a cost function(i,j) = d(x,y1.i|xj + 1.m) (1)That is(i,j) is the minimum cost of transforming the first j characters of x intothe first i characters of y. Then d(x,y) =(n,m). Now consider a sequence ofoperations S = (o 1 ,o 2 ,.,o k ) that trans

4、forms x to y with cost C(S) = d(x,y).Let S i be the subsequence of S containing the first i operations of S. Let z i be theauxilliary string after performing operations S i , where z 0 = x and z k = y.(d) We can calculate the edit distance d(x,y) using the definition o𝐜 𝐱𝐲 (i

5、,j) from Equation 1. Recall that d(x,y) = (m,n). Since we showed in part (a) that there existsa sequence of operations that achieves d(x,y) without using the “left” operation, weonly need to consider the four operations “right”, “replace”, “delete”, and “insert”.We can calculate (m,n) recursively. T

6、he base case is when no transformation operations have been performed, so 𝐜 (0,0) = 0.(e)Construct a table T where each entry Ti,j = (i,j). Since each valueof (i,j) only depends on 𝐜 (i1 ,j 2) wherei1= i andj1 𝟎 𝐚𝐧𝐝 𝐣𝐢 = 𝐲Ү

7、43;𝐓+ 𝟒 ,𝐢𝐓+ 𝟐 , 𝐢+ 𝟑5 return Tn,mThe running time of this algorithm is(mn). This algorithm requires(mn) space.(f) x: electrical engineeringy: computer scienceEdit Distance: 54Oper | c |Total| zinitial | 0 | 0 | *electrical engineeringdelete |

8、2 | 2 | *lectrical engineeringdelete | 2 | 4 | *ectrical engineeringdelete | 2 | 6 | *ctrical engineeringright | 0 | 6 | c*trical engineeringinsert | 3 | 9 | co*trical engineeringinsert | 3 | 12 | com*trical engineeringinsert | 3 | 15 | comp*trical engineeringinsert | 3 | 18 | compu*trical engineeri

9、ngright | 0 | 18 | comput*rical engineeringinsert | 3 | 21 | compute*rical engineeringright | 0 | 21 | computer*ical engineeringdelete | 2 | 23 | computer*cal engineeringdelete | 2 | 25 | computer*al engineeringdelete | 2 | 27 | computer*l engineeringdelete | 2 | 29 | computer* engineeringright | 0

10、| 29 | computer *engineeringdelete | 2 | 31 | computer *ngineeringreplace | 4 | 35 | computer s*gineeringreplace | 4 | 39 | computer sc*ineeringright | 0 | 39 | computer sci*neeringdelete | 2 | 41 | computer sci*eeringdelete | 2 | 43 | computer sci*eringright | 0 | 43 | computer scie*ringdelete | 2

11、| 45 | computer scie*ingdelete | 2 | 47 | computer scie*ngright | 0 | 47 | computer scien*ginsert | 3 | 50 | computer scienc*greplace | 4 | 54 | computer science*(g)Input File d(x, y)Input 1 1816Input 2 1824Input 3 1829(h)All the transformation operations can be implemented in O(1) time usingtwo sta

12、cks, L and R. Initially, L is empty and R contains all the characters of x inorder.Operation Implementationleft If not E MPTY (L), then P USH (R, P OP (L)right If not E MPTY (R), then P USH (L, P OP (R)replace by c If not E MPTY (R), then P OP (R), P USH (L, c)delete If not E MPTY (R), then P OP (R)

13、insert c P USH (L, c)Each stack operation requires O(1) time, and each transformation operation requiresonly O(1) stack operations. Therefore each operation requires O(1) time.代码部分:#include stdio.hstdlib.hstring.h/ Operation Costs#define MOVE_COST 0#define REPLACE_COST 4#define INSERT_COST 3#define

14、DELETE_COST 2/ Operation Typestypedef enum initial, right, insert, delete, replace, final opType;char* opStrings = initial, rightinsertdeletereplacefinal;/ Recovers and prints out the edits necessary to change x into y.static void recoverEdits(char* x, int m,char* y, int n,int* d, opType* opPerforme

15、d);/ Computes the edit distance between x and ystatic void computeEditDistance(char* x, int m,char* y, int n) / In this dynamic program, dij stores the edit distance between / the string xi.m-1 and yj.n-1. / NOTE! C arrays start indexing at 0.int* d;opType* opPerformed; / store operations for recons

16、tructing the answeropType opDone;int i, j;d = (int*)malloc(m+1)*sizeof(int*);assert(d != NULL);opPerformed = (opType*)malloc(m+1)*sizeof(opType*);assert(opPerformed !for (i = 0; i = m; i+) di = (int*)malloc(n+1)*sizeof(int);assert(di !opPerformedi = (opType*)malloc(n+1)*sizeof(opType);assert(opPerfo

17、rmedi !/ Initialize the base cases./ xm and yn are both null strings. Edit distance between them is 0.dmn = 0;opPerformedmn = final; m;/ To convert xi.m-1 to the null string yn, delete all (m-i)/ remaining characters in x.din = DELETE_COST*(m-i);opPerformedin = delete;for (j = 0; j =0; i-) for (j =

18、n-1; j = 0; j-) int costForReplaceOrMove;int costForInsert;int costForDelete;int minValue;/ Compute dij as the minimum of 4 terms:/ If xi = yj, we could move right./ Otherwise, we can replace xi with yj and/ increment i and j.costForReplaceOrMove = di+1j+1 + REPLACE_COST*(xi != yj) + MOVE_COST*(xi =

19、 yj);/ If we insert a character into x to match yj, then/ we increment j by 1costForInsert = dij+1 + INSERT_COST;/ If we delete a character in x, then we/ increment i by 1.costForDelete = di+1j + DELETE_COST;/ Of the above operations, find one that gives us/ a minimum cost.minValue = costForReplaceO

20、rMove;if (xi != yj)opDone = replace;else opDone = right;if (minValue costForInsert) minValue = costForInsert;opDone = insert; costForDelete) minValue = costForDelete;opDone = delete;dij = minValue;opPerformedij = opDone;/ Final answerprintf(Edit Distance = %dn, d00);/ Recover operations / print out

21、answer only if the strings arent too long.if (m 75) & (n 75) recoverEdits(x, m, y, n, d, opPerformed);free(di);free(opPerformedi);free(d);free(opPerformed); int* d, opType* opPerformed) int i = 0; int j = 0;int k = 0;int a;int opDone;int costSoFar = 0;int stepCost = 0;char* newString;newString = (ch

22、ar*)malloc(sizeof(char) * (m + n);/ Rather than implementing a string object for x that allows us/ constant-time inserts and deletes at the cursor, / we are just going to have two copies of the string./ newString0.k stores everything before the cursor that we have/ changed, and xi.m-1 will store all

23、 the parts after the cursor that/ are still the same.strncpy(newString, x, m);n);( i, j): %8s | c | Total | z nOper-n(%2d, %2d): %8s | %4d | %4d | , i, j, , costSoFar, stepCost);*%sn, x);while (opPerformedij != final) opDone = opPerformedij;switch(opDone) case right:newStringk = xi;i+;j+;stepCost = MOVE_COST;break;case replace:newStringk = yj;stepCost = REPLACE_COST;case insert:stepCost = INSERT_COST;case delete:stepCost = DELETE_COST;default: / We should never a follow a pointer to initial or final.ERROR.nexit(1);if (opDone != delete) k+;costSoFar += stepCost; %8s |

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1