HW 2 solution comments Problem 1 (Page 15, problem 11) –Matching with a set S rather than a string P –Crucial ideas Use 2 pointers to walk through the text T –p,q Maintain 2 “sets” or arrays –S[a] maintains number of a’s in set S –s[a] maintains number of a’s in T[p..q]
Problem 2: Pg 30, prob 12 Overall ok though “evaluating” effectiveness should imply testing correctness –Can you come up with a pattern and text where the bad character rule will have a better shift than normal KMP? –It must be the case that the bad character does not appear to the left of the mismatch in P
Impossible for bad character to be better for KMP if bad character is in P to the left of y –T = …x 1 x 2 x 3 x 4 x 5 x 6 x 7 y… – p 1 p 2 p 3 p 4 p 5 p 6 p 7 p 8 pattern P matches T between x 1 and x 7 but p 8 != y Lets say bad character shifts p 5 to be under y –T = …x 1 x 2 x 3 x 4 x 5 x 6 x 7 y… – …p 1 p 2 p 3 p 4 p 5 p 6 p 7 y… –However, normal KMP shifts p 6 to y –T = …x 1 x 2 x 3 x 4 x 5 x 6 x 7 y… – p 1 p 2 p 3 p 4 p 5 p 6 p 7 y… –This means that p 5 = x 7 = p 7 and bad character rule should shift p 7 to y
Problem 3: Pg 30, prob 13 I gave people full credit since it was a typo on the problem, but you still should have considered the real problem which is: –sp i = i - l i + 1 Is this true? –Problem is ambiguity of l i
Problem 3: Pg 30, prob 15 sp i values from sp i ’ values and vice versa –sp i values from sp i ’ values algorithm given on page 27 –sp i ’ values from sp i values First thought –sp i > sp i »sp i ’ = sp i –sp i = sp i »sp i ’ = 0?
sp i ’ values from sp i values sp i = sp i –sp i ’ = 0? – aabaab –sp i –sp i ’ Are we stuck then? –No, come up with a way to compute sp i ’
sp i ’ values from sp i values Use a Z-box recursive approach j = i; while ((j>1) && (sp j == sp j+1 - 1)) –j = sp j if (j > 1) sp i ’ = sp j ; else sp i ’ = 0;
Simpler approach for (i = 2; i<=m; i++) –if (sp i ’ != sp i+1 - 1) sp i ’ = sp i ; –else { j = sp i ; sp i ’ = sp j ’; /* define sp 1 ’ = sp 0 ’ = 0 */ –}–}
Problem 2: Pg 68, prob 14 People did well coming up with the right type of instance –P = {a, aa, aaa, aaaa, …, a r } –T = a m However, the analysis of k as a function of m and n = r(r+1)/2 was not very good