


"""simple docstring"""







from __future__ import annotations

from collections import deque






class 				__A    :






 """simple docstring"""




 def __init__(		self			,   __A						) ->					Any:
     a					=[]
     self.adlist.append(
         {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []}						)

     for keyword in keywords:
         self.add_keyword(__A						)
     self.set_fail_transitions()


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A						) ->					int | None:
     for state in self.adlist[current_state]["next_states"]:
         if char == self.adlist[state]["value"]:
             return state
     return None


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					None:
     a					=0
     for character in keyword:
         a					=self.find_next_state(__A			,   __A						)
         if next_state is None:
             self.adlist.append(
                 {
                     '''value''': character,
                     '''next_states''': [],
                     '''fail_state''': 0,
                     '''output''': [],
                 }						)
             self.adlist[current_state]["next_states"].append(len(self.adlist						) - 1						)
             a					=len(self.adlist						) - 1
         else:
             a					=next_state
     self.adlist[current_state]["output"].append(__A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					None:
     a					=deque()
     for node in self.adlist[0]["next_states"]:
         q.append(__A						)
         a					=0
     while q:
         a					=q.popleft()
         for child in self.adlist[r]["next_states"]:
             q.append(__A						)
             a					=self.adlist[r]['''fail_state''']
             while (
                 self.find_next_state(__A			,   self.adlist[child]['''value''']						) is None
                 and state != 0
             ):
                 a					=self.adlist[state]['''fail_state''']
             a					=self.find_next_state(
                 __A			,   self.adlist[child]['''value''']						)
             if self.adlist[child]["fail_state"] is None:
                 a					=0
             a					=(
                 self.adlist[child]['''output''']
                 + self.adlist[self.adlist[child]['''fail_state''']]['''output''']
             )





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					dict[str, list[int]]:
     a					={}  # returns a dict with keywords and list of its occurrences
     a					=0
     for i in range(len(__A						)						):
         while (
             self.find_next_state(__A			,   string[i]						) is None
             and current_state != 0
         ):
             a					=self.adlist[current_state]['''fail_state''']
         a					=self.find_next_state(__A			,   string[i]						)
         if next_state is None:
             a					=0
         else:
             a					=next_state
             for key in self.adlist[current_state]["output"]:
                 if key not in result:
                     a					=[]
                 result[key].append(i - len(__A						) + 1						)
     return result


if __name__ == "__main__":
       import doctest

       doctest.testmod()



"""simple docstring"""







from __future__ import annotations






class 				__A    :






 """simple docstring"""




 def __init__(		self			,   __A = 0						) ->					Dict:
     a					=key


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A						) ->					list[str]:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     a					=key or self.__key or 1

     # make sure key is an appropriate size
     key %= 255

     return [chr(ord(__A						) ^ key						) for ch in content]


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A						) ->					list[str]:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     a					=key or self.__key or 1

     # make sure key is an appropriate size
     key %= 255

     return [chr(ord(__A						) ^ key						) for ch in content]


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A = 0						) ->					str:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     a					=key or self.__key or 1

     # make sure key can be any size
     while key > 255:
         key -= 255

     # This will be returned
     a					=''''''

     for ch in content:
         ans += chr(ord(__A						) ^ key						)

     return ans


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A = 0						) ->					str:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     a					=key or self.__key or 1

     # make sure key can be any size
     while key > 255:
         key -= 255

     # This will be returned
     a					=''''''

     for ch in content:
         ans += chr(ord(__A						) ^ key						)

     return ans


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A = 0						) ->					bool:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     try:
         with open(__A						) as fin, open('''encrypt.out'''			,   '''w+'''						) as fout:
             # actual encrypt-process
             for line in fin:
                 fout.write(self.encrypt_string(__A			,   __A						)						)

     except OSError:
         return False

     return True





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A						) ->					bool:
     assert isinstance(__A			,   __A						) and isinstance(__A			,   __A						)

     try:
         with open(__A						) as fin, open('''decrypt.out'''			,   '''w+'''						) as fout:
             # actual encrypt-process
             for line in fin:
                 fout.write(self.decrypt_string(__A			,   __A						)						)

     except OSError:
         return False

     return True


# Tests
# crypt = XORCipher()
# key = 67

# # test encrypt
# print(crypt.encrypt("hallo welt",key))
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))

# if (crypt.encrypt_file("test.txt",key)):
#       print("encrypt successful")
# else:
#       print("encrypt unsuccessful")

# if (crypt.decrypt_file("encrypt.out",key)):
#       print("decrypt successful")
# else:
#       print("decrypt unsuccessful")