class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end_of_word = True
def search(self, word):
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end_of_word
def starts_with_prefix(self, prefix):
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True
class CrypticStateMachine:
def __init__(self):
self.trie = Trie()
def encrypt(self, message, shift):
"""
Encrypts the message using Caesar cipher with the given shift.
"""
encrypted_message = ""
for char in message:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
encrypted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
encrypted_message += encrypted_char
self.trie.insert(encrypted_char)
else:
encrypted_message += char
return encrypted_message
def decrypt(self, encrypted_message, shift):
"""
Decrypts the encrypted message using Caesar cipher with the given shift.
"""
decrypted_message = ""
for char in encrypted_message:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
decrypted_message += chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
else:
decrypted_message += char
return decrypted_message
def all_encryptions(self, message):
"""
Generates all possible encrypted messages for the given input message.
"""
for shift in range(26):
yield self.encrypt(message, shift)
def search_encrypted_message(self, prefix):
"""
Searches for encrypted messages starting with the given prefix.
"""
return self.trie.starts_with_prefix(prefix)
# Usage
cryptic_state_machine = CrypticStateMachine()
message = "I never said she stole my money"
all_encrypted_messages = list(cryptic_state_machine.all_encryptions(message))
for i, encrypted_message in enumerate(all_encrypted_messages):
print(f"Shift {i}: {encrypted_message}")
# Example search for encrypted messages starting with a prefix
print(cryptic_state_machine.search_encrypted_message("J of")) # True
print(cryptic_state_machine.search_encrypted_message("X il")) # False