Shil is now finally in a relationship with Uttu. Both of them like to exchange love…
- User
Shil is now finally in a relationship with Uttu. Both of them like to exchange love letters. However, to avoid exposing their relationship, they use "encryption" to send their messages. They use the famous Caesar cipher to encrypt their messages, which mathematically explained is as follows:
Encryption of a letter x by a shift n can be described mathematically as,
En(x) = (x + n) mod 26
For example: The shift of letter 'a' by 2 will give letter 'c'. Shift of letter 'z' by 1 will give 'a'.
In sent message, the original letter will be replaced by encrypted letter.
Recently, Shil sent a message to Uttu. However, he forgot to mention the shift n, which would have helped Uttu to decrypt the message. However, Uttu is sure that his beloved will send him a message which would be lexicographically minimum, when decrypted.
Help Uttu to decrypt the message by choosing appropriate shift n, that will give lexicographically minimum original message, among all possible messages. Your task is to find the decrypted message for Uttu. INPUT
First line will contain T, the number of messages you need to decrypt. Next T lines will contain a string S, the message sent by Shil to Uttu.
OUTPUT
You have to output T lines, i^th line containing the answer to the i^th message.
CONSTRAINTS
T ≤ 10^6 |S| ≤ 10^6
All characters in input message consist of only lowercase latin characters ('a'-'z').
Total number of characters in a test file ≤ 10^6
SAMPLE INPUT 1 phqghumeay
SAMPLE OUTPUT asbrsfxplj
Assistant
pythonimport sys def decrypt_message(s): possible_shifts = list(range(26)) n = len(s) for i in range(n): current_char = ord(s[i]) - ord('a') min_val = 26 # Find the minimum decrypted value for current_char among possible_shifts for shift in possible_shifts: decrypted = (current_char - shift) % 26 if decrypted < min_val: min_val = decrypted # Collect shifts that produce min_val new_shifts = [] for shift in possible_shifts: if (current_char - shift) % 26 == min_val: new_shifts.append(shift) possible_shifts = new_shifts if len(possible_shifts) == 1: break shift = possible_shifts[0] # Decrypt the entire string with the determined shift decrypted = [] for c in s: decrypted_char = chr( ((ord(c) - ord('a') - shift) % 26) + ord('a') ) decrypted.append(decrypted_char) return ''.join(decrypted) def main(): input = sys.stdin.read().split() T = int(input[0]) for i in range(1, T+1): s = input[i].strip() print(decrypt_message(s)) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.