联系我们: 手动添加方式: 微信>添加朋友>企业微信联系人>13262280223 或者 QQ: 1483266981
In many projects, it is important for programmers to have fine control over low-level details of program execution, and to be able to assess the cost of a design decision on likely overall program performance. This subject introduces students to a system programming language that gives programmers this kind of control, explores a range of standard data structures and algorithmic techniques, and shows how to apply them to frequently encountered problems.
这是一份unimelb墨尔本大学COMP20007 的成功案例
算法设计|Design of Algorithms COMP20007
问题 1.
Suppose you are given a source siring S|0 ldots, 1-1|S|0 ldots, 1-1| of length n, consisting of symbols a and b. Suppose further that you are given a pallern siring P(1) . . m-1 midP(1) . . m-1 mid of length mm ?. 7 , consisting of symbols a, ba, b, and *, representing a pattern to be found in string. SS. The symbol = is a “wild card” symbol, which matches a sangle symbol, either a or b. The other symbols must match exactly.The problem is tos output a sorted list MM of valid “match positions”, which are positions JJ in SS such that pattern PP matehes the sulstring. S|j-j+| P|-1|S|j-j+| P|-1|. For example, if S=a b a b b a bS=a b a b b a b and P=a b *P=a b *, then the output MM should be [1,2 mid[1,2 mid.
(a) [Descrile a straightforward, naive algorithen to solve the problem. Your algorithm sheuld run in time O(O( rern )).
证明 .
:One can explicitly check every possible starting position s∈{0,1, . . . , n?m}by checking whether each entry in Pmatches from sto s+m?1. NAIVE-ALGORITHM(S,P)1 M=[ ]2 fors= 0ton?m3 valid=TRUE 4 forj= 0tom?15 ifP[j]=?and P[j]=S[s+j]6 valid=FALSE 7 ifvalid8 M.APPEND(s)9 returnM 6.046J/18.410J
问题 2.
Solution: Let’s represent aa by 1, b1, b by -1-1, and * by 0 . We will use these reprecentatoons instead of the original symbols in this solution.
Notice that PP matches SS starting from position j, 0 leq j leq n-mj, 0 leq j leq n-m, if and only if for every 2,0 leq i leq m-12,0 leq i leq m-1, either P(i mid=0P(i mid=0 or S|j+i| P|2|=1S|j+i| P|2|=1. This is true if and only if sum_{i=0}^{m-1} S|j+i| P|i|=k sum_{i=0}^{m-1} S|j+i| P|i|=k where kk is the number of non-* symbols in PP.We would lake to express these sumnations as coefficicnts of a product of polynomials. Let xx be a variable. Represent SS as f s(x)=S|v|+S|1| x+ cdots+S|r u-1| x^{n-1} f s(x)=S|v|+S|1| x+ cdots+S|r u-1| x^{n-1}
证明 . Represent PP as left.f_{P}(x)=Q[0]+Q|1| x+ cdots+Q mid m-1 right] x^{m-1} left.f_{P}(x)=Q[0]+Q|1| x+ cdots+Q mid m-1 right] x^{m-1} where each Q|i|=P[m-1-i midQ|i|=P[m-1-i mid. Thus, we have reversed the order of the coefficients in this last representation.
Suppose CC is the product of the SS polynomial and the PP polynomial. Then the coefficient of x^{m-1+1}x^{m-1+1} in CC is sum_{i=1}^{m-1} S|m-1+j-i| Q|z|{1} sum_{i=1}^{m-1} S|m-1+j-i| Q|z|{1} which is equsl co sum{i=0}^{m-1} S|i+j| P|i| . sum{i=0}^{m-1} S|i+j| P|i| . Thes is the sane as the sum above. To obtain the output MM, we simply examine all the coefficients of CC, outputfing position number j, 0 leq j leq n-mj, 0 leq j leq n-m, exactly if the coefficient of x^{m-1+3}x^{m-1+3} is equal to kk, the total number of non-ksymbols in PP. We cuecput. these in order of increasing jj, as required.In the example alowe, SS is represented by int_{S}(x)=1-x+x^{2}-x^{3}-x^{4}+x^{3}-x^{6} int_{S}(x)=1-x+x^{2}-x^{3}-x^{4}+x^{3}-x^{6}
and PP by f x(x)=-x+x^{2} f x(x)=-x+x^{2} The product CC is -x+2 x^{2}-2 x^{3}+2 x^{4}-2 x^{6}+2 x^{7}-x^{x} . -x+2 x^{2}-2 x^{3}+2 x^{4}-2 x^{6}+2 x^{7}-x^{x} . The number kk is equal to 2 , so the terms of interest are 2 x^{2}, 2 x^{4}2 x^{2}, 2 x^{4}, and 2 x^{2}2 x^{2}. These would yield j=0,2,5j=0,2,5, but 5 is ruled out because we are only considering j leq n- pi n=j leq n- pi n= 7-3=47-3=4.


发表回复