Assignment 09 - String Matching (Solution)

Assignment 09 - String Matching (Solution) - The Hong Kong...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Solution to Assignment 9 Question 1 : Give a linear-time algorithm to determine if a text T is a cyclic rotation of another string T ′ . For example, arc and car are cyclic rotations of each other. (Hint: Use the next function in the KMP algorithm.) Solution 1: Assume that both T and T ′ are of length n . It is a necessary condition for T and T ′ to be cyclic rotations to each other. And it can be checked in linear time. We then construct the text and pattern for KMP algorithm: • Text: TT (of length 2 n ) • Pattern: T ′ (of length n ) Afterwards, we run KMP algorithm on the constructed text and pattern which will takes O ( n + 2 n ) time. We then analyze the output: • If there exists an occurrence of the pattern in the text with shift s . This situation is shown in the following figure T T s X 1 X 1 X 2 X 2 X 2 X 1 T' According to the figure, T ′ can be written as T ′...
View Full Document

Page1 / 2

Assignment 09 - String Matching (Solution) - The Hong Kong...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online