eolymp
bolt
Try our new interface for solving problems
Problems

Hidden Code

Hidden Code

It's time to put your hacking skills to the test! You've been called upon to help crack enemy codes in the current war on... something or another. Anyway, the point is that you have discovered the encryption technique used by the enemy; it is quite simple, and proceeds as follows. Note that all strings contain only uppercase letters of the alphabet. \begin{enumerate} \item We are given a key \textbf{K} and a plaintext \textbf{P} which is encrypted character-by-character to produce a ciphertext \textbf{C} of the same length. \item If |\textbf{K}| is the length of the key \textbf{K}, then the first |\textbf{K}| characters of \textbf{C} are obtained by adding the first |\textbf{K}| characters of \textbf{P} to the characters of \textbf{K}, where adding two letters means interpreting them as numbers (\textbf{A = 0}, \textbf{B = 1}, and so on) and taking the sum modulo \textbf{26}. That is, \textbf{C_i = (P_i + K_i ) mod 26} for \textbf{i = 1, ... ,|K|}. If |\textbf{K}| > |\textbf{P}|, then the extra characters in \textbf{K} are ignored. \item The remaining characters of \textbf{P}, i.e. \textbf{P_i} for \textbf{i} > |\textbf{K}|, are encrypted using the previous ciphertext characters by \textbf{Ci = (P_i + C_\{i−|K|\}) mod 26} for \textbf{i = |K| + 1, ..., |P|}. \end{enumerate} As an example, consider the encryption of the string "\textbf{STANFORD}" using the key "\textbf{ACM}": \textbf{STA NFORD + ACM SVMFA ---------- SVM FAAWD} Knowing this, you are well on your way to being able to read the enemy's communications. Luckily, you also have several pairs of plaintexts and ciphertexts which your team recovered, all of which are known to be encrypted with the same key. Help find the key that the enemy is using. \InputFile The input consists of multiple test cases. Each test case begins with a line containing a single integer \textbf{n}, \textbf{1} ≤ \textbf{n} ≤ \textbf{100}, the number of plaintext and ciphertext pairs you will receive. The next \textbf{n} lines each contain two strings \textbf{P} and \textbf{C}, the plaintext and ciphertext, respectively. \textbf{P} and \textbf{C} will contain only uppercase letters (\textbf{A}-\textbf{Z}) and have the same length (at most \textbf{100} characters). The input terminates with a line with \textbf{n = 0}. \OutputFile For each test case, print a single line that contains the shortest possible key or "\textbf{Impossible}" (quotes added for clarity) if no possible key could have produced all of the encryptions.
Time limit 1 second
Memory limit 64 MiB
Input example #1
1
A B
3
STANFORD SVMFAAWD
AVOWIENR AXAWFEJW
VAMRI VCYMK
3
ABCDEFGHIJKLMNOPQRSTUVWXYZ AAAAAAAAAAAAAAAAAAAAAAAAAA
Y Y
Z Z
2
A B
B A
0
Output example #1
B
ACM
AZYXWVUTSRQPONMLKJIHGFEDCB
Impossible
Source 2011 Stanford Local ACM Programming Contest, Saturday, October 8th, 2011