python - Construct an example where the KMP algorithm has to backtrack multiple times -
i'm trying construct example kmp algorithm computelpsarray phase have backtrack (see comment below) multiple times (cell in lps array).
e.g. 'aaacaaa' visits backtrack section twice i = 3 , once i = 7
can me construct string visit backtrack section 3-4 times i?
def computelpsarray(pat, m, lps): len = 0 # length of previous longest prefix suffix lps[0]=0 # lps[0] 0 = 1 while < m: if pat[i]==pat[len]: len+=1 lps[i] = len i+=1 else: if len!=0: # backtrack section - when here 3-4 times same i??? len = lps[len-1] else: lps[i] = 0 i+=1
with 'aaaacaa' visits backtrack section 3 times i = 4.
with 'aaaaaca' visits backtrack section 4 times i = 5.
Comments
Post a Comment