### hiukim's blog

By hiukim, 7 years ago, So I'm studying the failure function from this topcoder tutorial: http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=stringSearching

and it said "given a string (a quite long one), find all its proper suffixes that are also prefixes of it. All we have to do is just to calculate the "failure function" of the given string and using the information stored in it to print the answer."

I couldn't figure out how I can do that with the failure function. Can anyone give some ideas?  Comments (1)
 » Suppose u are searching for pattern P=abac in text T=ababac. let i be the index moving through P and j be the index moving through T and they both start from 0.So,P=T increment both i and j by 1. i=1, j=1P=T increment both i and j by 1. i=2, j=2 .... P not equals T, but lets see what information we have got with us, we know that we have seen aba which means we have seen the suffixes of aba as well ba and a. Out of which ba is not a prefix of P so it is of no use but turns out a is a prefix of P so from now on we will try to extend pattern from index 1, i=1,j=3 (This is exactly what the failure function stores!)P=T(bingo!) increment both i and j. i=2, j=4Similarly,P=T.P=T.Match found!