






'''simple docstring'''







from abc import ABC, abstractmethod
from typing import List, Optional




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		def __init__(   self							):
								# test for the above condition
								self.test()






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	0
								lowerCamelCase__								=	False
								while not completed:
														if counter == 1:
																				self.reset()
														lowerCamelCase__								=	self.advance()
														if not self.does_advance(_lowerCAmelCase							):
																				raise Exception(
																				    """Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."""							)

														lowerCamelCase__							,      lowerCamelCase__							,      lowerCamelCase__								=	self.update(_lowerCAmelCase							)
														counter += 1

														if counter > 1_00_00:
																				raise Exception("""update() does not fulfill the constraint."""							)

								if self.remaining() != 0:
														raise Exception("""Custom Constraint is not defined correctly."""							)






		@abstractmethod
		def       UpperCamelCase_       (   self							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)






		@abstractmethod
		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)






		@abstractmethod
		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)






		@abstractmethod
		def       UpperCamelCase_       (   self							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)






		@abstractmethod
		def       UpperCamelCase_       (   self							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)






		@abstractmethod
		def       UpperCamelCase_       (   self ,_lowerCAmelCase=False							):
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''							)




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase							):
								super(_lowerCAmelCase ,self							).__init__()

								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							) or len(_lowerCAmelCase							) == 0:
														raise ValueError(F'''`token_ids` has to be a non-empty list, but is {token_ids}.'''							)
								if any((not isinstance(_lowerCAmelCase ,_lowerCAmelCase							) or token_id < 0) for token_id in token_ids							):
														raise ValueError(F'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'''							)

								lowerCamelCase__								=	token_ids

								lowerCamelCase__								=	len(self.token_ids							)
								lowerCamelCase__								=	-1  # the index of the currently fulfilled step
								lowerCamelCase__								=	False






		def       UpperCamelCase_       (   self							):
								if self.completed:
														return None
								return self.token_ids[self.fulfilled_idx + 1]






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(F'''`token_id` has to be an `int`, but is {token_id} of type {type(_lowerCAmelCase							)}'''							)

								if self.completed:
														return False

								return token_id == self.token_ids[self.fulfilled_idx + 1]






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(F'''`token_id` has to be an `int`, but is {token_id} of type {type(_lowerCAmelCase							)}'''							)

								lowerCamelCase__								=	False
								lowerCamelCase__								=	False
								lowerCamelCase__								=	False

								if self.does_advance(_lowerCAmelCase							):
														self.fulfilled_idx += 1
														lowerCamelCase__								=	True
														if self.fulfilled_idx == (self.seqlen - 1):
																				lowerCamelCase__								=	True
														lowerCamelCase__								=	completed
								else:
														# failed to make progress.
														lowerCamelCase__								=	True
														self.reset()
								return stepped, completed, reset






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	False
								lowerCamelCase__								=	0






		def       UpperCamelCase_       (   self							):
								return self.seqlen - (self.fulfilled_idx + 1)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase=False							):
								lowerCamelCase__								=	PhrasalConstraint(self.token_ids							)

								if stateful:
														lowerCamelCase__								=	self.seqlen
														lowerCamelCase__								=	self.fulfilled_idx
														lowerCamelCase__								=	self.completed

								return new_constraint




class 		UpperCamelCase__     :




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase ,_lowerCAmelCase=True							):
								lowerCamelCase__								=	max([len(_lowerCAmelCase							) for one in nested_token_ids]							)

								lowerCamelCase__								=	{}
								for token_ids in nested_token_ids:
														lowerCamelCase__								=	root
														for tidx, token_id in enumerate(_lowerCAmelCase							):
																				if token_id not in level:
																										lowerCamelCase__								=	{}

																				lowerCamelCase__								=	level[token_id]

								if no_subsets and self.has_subsets(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(
														    """Each list in `nested_token_ids` can't be a complete subset of another list, but is"""
														    F''' {nested_token_ids}.'''							)

								lowerCamelCase__								=	root






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.trie

								for current_token in current_seq:
														lowerCamelCase__								=	start[current_token]

								lowerCamelCase__								=	list(start.keys()							)

								return next_tokens






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.next_tokens(_lowerCAmelCase							)

								return len(_lowerCAmelCase							) == 0






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	list(root.values()							)
								if len(_lowerCAmelCase							) == 0:
														return 1
								else:
														return sum([self.count_leaves(_lowerCAmelCase							) for nn in next_nodes]							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.count_leaves(_lowerCAmelCase							)
								return len(_lowerCAmelCase							) != leaf_count




class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase							):
								super(_lowerCAmelCase ,self							).__init__()

								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							) or len(_lowerCAmelCase							) == 0:
														raise ValueError(F'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'''							)
								if any(not isinstance(_lowerCAmelCase ,_lowerCAmelCase							) for token_ids in nested_token_ids							):
														raise ValueError(F'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'''							)
								if any(
								    any((not isinstance(_lowerCAmelCase ,_lowerCAmelCase							) or token_id < 0) for token_id in token_ids							)
								    for token_ids in nested_token_ids							):
														raise ValueError(
														    F'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'''							)

								lowerCamelCase__								=	DisjunctiveTrie(_lowerCAmelCase							)
								lowerCamelCase__								=	nested_token_ids

								lowerCamelCase__								=	self.trie.max_height
								lowerCamelCase__								=	[]
								lowerCamelCase__								=	False






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.trie.next_tokens(self.current_seq							)

								if len(_lowerCAmelCase							) == 0:
														return None
								else:
														return token_list






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(F'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_lowerCAmelCase							)}'''							)

								lowerCamelCase__								=	self.trie.next_tokens(self.current_seq							)

								return token_id in next_tokens






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(F'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_lowerCAmelCase							)}'''							)

								lowerCamelCase__								=	False
								lowerCamelCase__								=	False
								lowerCamelCase__								=	False

								if self.does_advance(_lowerCAmelCase							):
														self.current_seq.append(_lowerCAmelCase							)
														lowerCamelCase__								=	True
								else:
														lowerCamelCase__								=	True
														self.reset()

								lowerCamelCase__								=	self.trie.reached_leaf(self.current_seq							)
								lowerCamelCase__								=	completed

								return stepped, completed, reset






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	False
								lowerCamelCase__								=	[]






		def       UpperCamelCase_       (   self							):
								if self.completed:
														# since this can be completed without reaching max height
														return 0
								else:
														return self.seqlen - len(self.current_seq							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase=False							):
								lowerCamelCase__								=	DisjunctiveConstraint(self.token_ids							)

								if stateful:
														lowerCamelCase__								=	self.seqlen
														lowerCamelCase__								=	self.current_seq
														lowerCamelCase__								=	self.completed

								return new_constraint




class 		UpperCamelCase__     :




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	constraints

								# max # of steps required to fulfill a given constraint
								lowerCamelCase__								=	max([c.seqlen for c in constraints]							)
								lowerCamelCase__								=	len(_lowerCAmelCase							)
								lowerCamelCase__								=	False

								self.init_state()






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	[]
								lowerCamelCase__								=	None
								lowerCamelCase__								=	[constraint.copy(stateful=_lowerCAmelCase							) for constraint in self.constraints]






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	0
								if self.inprogress_constraint:
														# extra points for having a constraint mid-fulfilled
														add += self.max_seqlen - self.inprogress_constraint.remaining()

								return (len(self.complete_constraints							) * self.max_seqlen) + add






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	[]
								if self.inprogress_constraint is None:
														for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																				lowerCamelCase__								=	constraint.advance()
																				if isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
																										token_list.append(_lowerCAmelCase							)
																				elif isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
																										token_list.extend(_lowerCAmelCase							)
								else:
														lowerCamelCase__								=	self.inprogress_constraint.advance()
														if isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
																				token_list.append(_lowerCAmelCase							)
														elif isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
																				token_list.extend(_lowerCAmelCase							)

								if len(_lowerCAmelCase							) == 0:
														return None
								else:
														return token_list






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								self.init_state()

								if token_ids is not None:
														for token in token_ids:
																				# completes or steps **one** constraint
																				lowerCamelCase__							,      lowerCamelCase__								=	self.add(_lowerCAmelCase							)

																				# the entire list of constraints are fulfilled
																				if self.completed:
																										break






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								if not isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														raise ValueError(F'''`token_id` should be an `int`, but is `{token_id}`.'''							)

								lowerCamelCase__							,      lowerCamelCase__								=	False, False

								if self.completed:
														lowerCamelCase__								=	True
														lowerCamelCase__								=	False
														return complete, stepped

								if self.inprogress_constraint is not None:
														# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
														# job, simply update the state

														lowerCamelCase__							,      lowerCamelCase__							,      lowerCamelCase__								=	self.inprogress_constraint.update(_lowerCAmelCase							)
														if reset:
																				# 1. If the next token breaks the progress, then we must restart.
																				#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																				#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																				#     constraint, not the full list of constraints.

																				self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_lowerCAmelCase							)							)
																				lowerCamelCase__								=	None

														if complete:
																				# 2. If the next token completes the constraint, move it to completed list, set
																				#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																				#     is complete.

																				self.complete_constraints.append(self.inprogress_constraint							)
																				lowerCamelCase__								=	None

																				if len(self.pending_constraints							) == 0:
																										# we're done!
																										lowerCamelCase__								=	True

								else:
														# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
														# of constraints?

														for cidx, pending_constraint in enumerate(self.pending_constraints							):
																				if pending_constraint.does_advance(_lowerCAmelCase							):
																										lowerCamelCase__							,      lowerCamelCase__							,      lowerCamelCase__								=	pending_constraint.update(_lowerCAmelCase							)

																										if not stepped:
																																raise Exception(
																																    """`constraint.update(token_id)` is not yielding incremental progress, """
																																    """even though `constraint.does_advance(token_id)` is true."""							)

																										if complete:
																																self.complete_constraints.append(_lowerCAmelCase							)
																																lowerCamelCase__								=	None

																										if not complete and stepped:
																																lowerCamelCase__								=	pending_constraint

																										if complete or stepped:
																																# If we made any progress at all, then it's at least not a "pending constraint".

																																lowerCamelCase__								=	(
																																    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																																)

																																if len(self.pending_constraints							) == 0 and self.inprogress_constraint is None:
																																						# If there's no longer any pending after this and no inprogress either, then we must be
																																						# complete.

																																						lowerCamelCase__								=	True

																																break  # prevent accidentally stepping through multiple constraints with just one token.

								return complete, stepped






		def       UpperCamelCase_       (   self ,_lowerCAmelCase=True							):
								lowerCamelCase__								=	ConstraintListState(self.constraints							)  # we actually never though self.constraints objects
								# throughout this process. So it's at initialization state.

								if stateful:
														lowerCamelCase__								=	[
														    constraint.copy(stateful=_lowerCAmelCase							) for constraint in self.complete_constraints
														]
														if self.inprogress_constraint is not None:
																				lowerCamelCase__								=	self.inprogress_constraint.copy(stateful=_lowerCAmelCase							)
														lowerCamelCase__								=	[constraint.copy() for constraint in self.pending_constraints]

								return new_state








"""simple docstring"""






def      lowercase__ (					lowerCAmelCase__   :			str			)    ->			list[int]:



			'''simple docstring'''
			a__  :	List[str]		 =			[0 for i in range(len(lowerCAmelCase__			)			)]

			# initialize interval's left pointer and right pointer
			a__ ,	a__  :	int		 =			0, 0

			for i in range(1							,			len(lowerCAmelCase__			)			):
						# case when current index is inside the interval
						if i <= right_pointer:
									a__  :	List[Any]		 =			min(right_pointer - i + 1							,			z_result[i - left_pointer]			)
									a__  :	List[str]		 =			min_edge

						while go_next(lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__			):
									z_result[i] += 1

						# if new index's result gives us more right interval,
						# we've to update left_pointer and right_pointer
						if i + z_result[i] - 1 > right_pointer:
									a__ ,	a__  :	str		 =			i, i + z_result[i] - 1

			return z_result






def      lowercase__ (					lowerCAmelCase__   :			int							,			lowerCAmelCase__   :			list[int]							,			lowerCAmelCase__   :			str			)    ->			bool:



			'''simple docstring'''
			return i + z_result[i] < len(lowerCAmelCase__			) and s[z_result[i]] == s[i + z_result[i]]






def      lowercase__ (					lowerCAmelCase__   :			str							,			lowerCAmelCase__   :			str			)    ->			int:



			'''simple docstring'''
			a__  :	List[Any]		 =			0
			# concatenate 'pattern' and 'input_str' and call z_function
			# with concatenated string
			a__  :	List[str]		 =			z_function(pattern + input_str			)

			for val in z_result:
						# if value is greater then length of the pattern string
						# that means this index is starting position of substring
						# which is equal to pattern string
						if val >= len(lowerCAmelCase__			):
									answer += 1

			return answer


if __name__ == "__main__":
		import doctest

		doctest.testmod()