This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 lineartime 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
This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.
 Spring '07
 ARYA
 Algorithms

Click to edit the document details