# HW9_sol - L ( k ) = max( { 1 + L ( j ) : k + 1 ≤ j ≤...

This preview shows pages 1–2. Sign up to view the full content.

Fall Semester 2008 CS300 Algorithms Solution #9 1. (a) Start 1 2 3 4 5 6 7 * A A B B A A B B B B B A A A Figure 1: Finite automaton (b) * A A B B A A B s s s s s s s s Get next text character f f f f f f f Figure 2: KMP 2. step1: If | X | 6 = | Y | then output “NO”. step2: Let P = x 1 x 2 ··· x n and T = y 1 y 2 ··· y n y 1 y 2 ··· y n . step3: Perform KMP algorithm for P (pattern) and T (text). step4: If the pattern matching is successful, output “YES”. Otherwise, output “NO”. Time complexity : O ( n ) + O (2 n + n ) = O ( n ) 3. Let L ( k ) be the length of the longest increasing subsequence of [ x k ,x k +1 , ··· ,x n ] that begins with x k , for 1 k n . Let S ( k ) be the index of the second element of this longest increasing subsequence (if there is a second element). We shall compute the values of L and S starting with n and working our way back to 1. L ( n ) = 1 and S ( n ) = nil . Let k < n . The longest increasing subsequence whose ﬁrst element is x k and whose second element is x j (assuming x j > x k and j > k ) has length 1 + L ( j ). Thus, the longest increasing subsequence of [ x k , ··· ,x m ] that begins with x k has length max( { 1 + L ( j ) : k + 1 j n,x j > x k } ∪ { 1 } ). So we have

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: L ( k ) = max( { 1 + L ( j ) : k + 1 ≤ j ≤ n,x j > x k } ∪ { 1 } ). If the maximum is achieved by 1 + L ( j ) for some j , save that value of j in S ( k ). If the maximum is 1, set S ( k ) to nil . Computing L ( k ) and S ( k ) for each k requires ﬁnding the maximum of at most n-k + 2 elements, which can be done in time proportional to n-k + 2. So ﬁlling in the L and S arrays takes O ( ∑ n-1 k =1 n-k + 3) = O ( n 2 ) time. To actually produce the longest increasing subsequence, we use the following recursive procedure. 1 printSequence(k) // prints longest increasing subsequence of [x_k,. ..,x_m] that starts at x_k print x_k if S(k) != nil then printSequence(S(k)) end printSequence The answer is obtained by calling printSequence(k) , where k is the index of the largest element of array L . This call requires O ( n ) time. 2...
View Full Document

## This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

### Page1 / 2

HW9_sol - L ( k ) = max( { 1 + L ( j ) : k + 1 ≤ j ≤...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online