Once upon a time chef decided to learn encodings. And, obviously, he started with the…
- User
Once upon a time chef decided to learn encodings. And, obviously, he started with the easiest one (well, actually the easiest after Caesar cypher) – substitution cypher. But very soon Chef got bored with encoding/decoding, so he started thinking how to hack this cypher. He already knows some algorithm, which is not always correct, but it’s sufficient for now. Here is its description. Imagine we know frequency sequence of English letters (this means, that letters are sorted by their frequency of appearing in English texts, in ascending order). And let’s find frequency sequence of cyphered letters (if some of them appear equal number of times, then first in frequency sequence will be lower letter between them). Now, using this two frequency sequences we can recover plain text. Just substitute cyphered letter with origin one, if they are at same positions in sequences. Now, Chef has frequency sequence of English letters and cypher text. And he asks you to recover plain text. Please, help him. Input In first line number T is given - number of test cases. Then T test cases follow. Each test case consists of two lines - frequency sequence and encrypted text.
Output For each test case you should output decrypted with the given frequency sequence text. Please note, that the case of letters should be preserved.
Constraints
1 ≤ T ≤ 1000; Length of frequency sequence is always 26; 1 ≤ length of the text ≤ 150000; 1 ≤ sum lengths of all texts ≤ 150000. Frequency sequence consists of all lowercase English letters. Text consists of any characters.
Example
Input: 3 qwrtyuipasdfgjkzxcvbnmheol dummy! bfgjklmopqrstuwxzhvnicdyea abcd b efgd hbi! qwrtyuipasdfgjkzxcvbnmheol Dummy!
Output: hello! have a nice day! Hello!
Assistant
import sys from collections import defaultdict def main(): t = int(sys.stdin.readline()) for _ in range(t): english = sys.stdin.readline().strip() cipher = sys.stdin.readline().rstrip('\n') freq = defaultdict(int) for c in cipher: if c.isalpha(): lower_c = c.lower() freq[lower_c] += 1 letters = [chr(ord('a') + i) for i in range(26)] sorted_letters = sorted(letters, key=lambda x: (freq[x], x)) mapping = {encrypted: english[i] for i, encrypted in enumerate(sorted_letters)} res = [] for c in cipher: if c.isalpha(): lower_c = c.lower() decrypted = mapping[lower_c] res.append(decrypted.upper() if c.isupper() else decrypted) else: res.append(c) print(''.join(res)) 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.