Similarity of necklaces 2

POJ - 2754