For those who want to learn more about this kind of algorithms on strings, there is a great book named, unsurprisingly, "Algorithms on Strings" (<a href="http://www.amazon.com/Algorithms-Strings-Maxime-Crochemore/dp/0521848997" rel="nofollow">http://www.amazon.com/Algorithms-Strings-Maxime-Crochemore/d...</a>).<p>Although I must admint, jogojapan has written a really clear and thoughtful explanation of Ukkonen's algorithm on stackoverflow, the best one I've ever read.