#P16994. [NWERC 2018] Kleptography

[NWERC 2018] Kleptography

Problem Description

John likes simple ciphers. He had been using the “Caesar” cipher to encrypt his diary until recently, when he learned a hard lesson about its strength by catching his sister Mary browsing through the diary without any problems.

Rapidly searching for an alternative, John found the famous “Autokey” cipher. He uses a version that takes the 2626 lower-case letters a--z and internally translates them in alphabetical order to the numbers 00 to 2525.

The encryption key kk begins with a secret prefix of nn letters. Each of the remaining letters of the key is copied from the letters of the plaintext aa, so that kn+i=aik_{n+i}=a_i for i1i\ge 1. Encryption of the plaintext aa to the ciphertext bb follows the formula bi=ai+kimod26b_i=a_i+k_i \bmod 26.

Mary was able to get a peek at the last nn letters John typed before he noticed her, encrypted the text document, and left. This could be her chance.

Input Format

The input consists of:

  • One line with two integers nn and mm (1n301\le n\le 30, n+1m100n+1\le m\le 100), where nn is the keyword length as well as the number of letters Mary saw, and mm is the length of the text.
  • One line with nn lower-case letters, the last nn letters of the plaintext.
  • One line with mm lower-case letters, the whole ciphertext.

Output Format

Output the plaintext of John's diary.

5 16
again
pirpumsemoystoal
marywasnosyagain
1 12
d
fzvfkdocukfu
shortkeyword