From 2353764017d42389166b99b7d150dec438d06035 Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Tue, 9 May 2023 16:57:35 +0200 Subject: [PATCH] Add files via upload --- fsa_pattern.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 fsa_pattern.py 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