@@ -62,32 +62,35 @@ def extend(self, char: str) -> None:
6262 curr = len (self .states )
6363 self .states .append (State (length = self .states [self .last ].length + 1 ))
6464
65- p = self .last
66- while p != - 1 and char not in self .states [p ].next :
67- self .states [p ].next [char ] = curr
68- p = self .states [p ].link
65+ prev_state = self .last
66+ while prev_state != - 1 and char not in self .states [prev_state ].next :
67+ self .states [prev_state ].next [char ] = curr
68+ prev_state = self .states [prev_state ].link
6969
70- if p == - 1 :
70+ if prev_state == - 1 :
7171 self .states [curr ].link = 0
7272 else :
73- q = self .states [p ].next [char ]
74- if self .states [p ].length + 1 == self .states [q ].length :
75- self .states [curr ].link = q
73+ next_state = self .states [prev_state ].next [char ]
74+ if self .states [prev_state ].length + 1 == self .states [next_state ].length :
75+ self .states [curr ].link = next_state
7676 else :
7777 clone = len (self .states )
7878 self .states .append (
7979 State (
80- length = self .states [p ].length + 1 ,
81- link = self .states [q ].link ,
80+ length = self .states [prev_state ].length + 1 ,
81+ link = self .states [next_state ].link ,
8282 )
8383 )
84- self .states [clone ].next = dict (self .states [q ].next )
84+ self .states [clone ].next = dict (self .states [next_state ].next )
8585
86- while p != - 1 and self .states [p ].next .get (char ) == q :
87- self .states [p ].next [char ] = clone
88- p = self .states [p ].link
86+ while (
87+ prev_state != - 1
88+ and self .states [prev_state ].next .get (char ) == next_state
89+ ):
90+ self .states [prev_state ].next [char ] = clone
91+ prev_state = self .states [prev_state ].link
8992
90- self .states [q ].link = clone
93+ self .states [next_state ].link = clone
9194 self .states [curr ].link = clone
9295
9396 self .last = curr
@@ -120,8 +123,8 @@ def count_distinct_substrings(self) -> int:
120123 4
121124 """
122125 total = 0
123- for i in range ( 1 , len ( self .states )) :
124- total += self . states [ i ]. length - self .states [self . states [ i ] .link ].length
126+ for state in self .states [ 1 :] :
127+ total += state . length - self .states [state .link ].length
125128 return total
126129
127130 def count_occurrences (self , pattern : str ) -> int :
@@ -148,7 +151,7 @@ def count_occurrences(self, pattern: str) -> int:
148151 occurrences = [0 ] * len (self .states )
149152 order = sorted (
150153 range (len (self .states )),
151- key = lambda i : self .states [i ].length ,
154+ key = lambda state_index : self .states [state_index ].length ,
152155 reverse = True ,
153156 )
154157
@@ -159,9 +162,9 @@ def count_occurrences(self, pattern: str) -> int:
159162 occurrences [temp_last ] = 1
160163
161164 # Push endpos sizes up the suffix link tree
162- for u in order :
163- if self .states [u ].link != - 1 :
164- occurrences [self .states [u ].link ] += occurrences [u ]
165+ for state_index in order :
166+ if self .states [state_index ].link != - 1 :
167+ occurrences [self .states [state_index ].link ] += occurrences [state_index ]
165168
166169 return occurrences [curr ]
167170
0 commit comments