diff --git a/fsa_pattern.py b/fsa_pattern.py new file mode 100644 index 00000000..4a966e5c --- /dev/null +++ b/fsa_pattern.py @@ -0,0 +1,77 @@ +# Python program for Finite State Automaton +# Pattern searching Algorithm + +NO_OF_CHARS = 256 + +def getNextState(pat, M, state, x): + ''' + calculate the next state + ''' + + # If the character c is same as next character + # in pattern, then simply increment state + + if state < M and x == ord(pat[state]): + return state+1 + + i=0 + # ns stores the result which is next state + + # ns finally contains the longest prefix + # which is also suffix in "pat[0..state-1]c" + + # Start from the largest possible value and + # stop when you find a prefix which is also suffix + for ns in range(state,0,-1): + if ord(pat[ns-1]) == x: + while(i