Failture Function

PLEASE, help me, i want to code Failture Function is define as the size of the largest prefix of the partern that is also suffix of the partial pattern ...by default" F[0]=-1
examble P='abacab'
image
and it not correct with P='aabaaab'
and my code:

def failture_FC(P):
    P.translate({ord(c): None for c in string.whitespace})
    F=[0]*(len(P))
    F[0]=-1
    i=2
    j=0
    for i in range(2, len(P)):
        if(P[i-1]==P[j]):
            F[i]=j+1
            i+=1
            j+=1
        elif j>0 :
            j=F[j-1]
        else:
            F[i]=0
            i+=1
    return  F
P=input("String  P: ")
failture_FC(P)
for i in range (len(failture_FC(P))):
      print(failture_FC(P)[i])

Thanks you helping <3

Comments (0)