Under construction

Still being built. Things may change

The Key to Cryptography

Time limit2sMemory limit512 MB

Summary
Given an autokey cipher's ciphertext and secret word, recover the original plaintext message.
Level

Easy3 of 10

Topics
String, Implementation
Solved
No attempts yet

Problem

Suppose you have to encrypt a top secret message such as "SEND MORE MONKEYS". You could use a simple substitution cipher that replaces each letter of the alphabet with a different letter, but ciphers like that break easily: some letters (E, S, A) turn up far more often than others (Q, Z, X). Varying the substitution applied to each letter is safer. The autokey cipher works that way.

To encrypt a message, first pick a secret word, say "ACM", and put it in front of the message. Truncate the longer string to the length of the message and call it the key. The nnth letter of the key encrypts the nnth letter of the original message. Each key letter is a cyclic shift amount: A means a shift of 0, B a shift of 1, and so on. With "ACM" as the secret word, the message is encrypted like this.

 SENDMOREMONKEYS (message)
 ACMSENDMOREMONK (key)
------------------------------
 SGZVQBUQAFRWSLC (ciphertext)

The letter E in the message became G the first time it appeared, because the matching key letter was C, a shift of 2. The next two times it became Q and then S.

Given a ciphertext and the secret word, recover the original message.

Input

The input has two lines. The first line holds the ciphertext and the second line holds the secret word. Both lines contain uppercase letters only. If the secret word is at least as long as the ciphertext, the key is the prefix of the secret word whose length equals the ciphertext length.

Output

Print the original message that produced the given ciphertext under the given secret word.

Examples5

  1. Example 1

    Input
    SGZVQBUQAFRWSLC
    ACM
    
    Expected output
    SENDMOREMONKEYS
    
  2. Example 2

    Input
    A
    A
    
    Expected output
    A
    
  3. Example 3

    Input
    YYYY
    Z
    
    Expected output
    ZZZZ
    
  4. Example 4

    Input
    HFNOS
    ABCDEFGHIJ
    
    Expected output
    HELLO
    
  5. Example 5

    Input
    LXFOPKTMDCGN
    LEMON
    
    Expected output
    ATTACKATDAWN