In cryptography, a Caesar cipher, also known as the shift cipher, is one of the most straightforward and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions up (or down) the alphabet.
For example, with the right shift of 1919, A would be replaced by T, B would be replaced by U, and so on. A full exhaustive list is as follows:
- The plaintext: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z;
- The ciphertext: T U V W X Y Z A B C D E F G H I J K L M N O P Q R S.
Now you have a plaintext and its ciphertext encrypted by a Caesar Cipher. You also have another ciphertext encrypted by the same method and are asked to decrypt it.
Input
The input contains several test cases, and the first line is a positive integer TTindicating the number of test cases which is up to 5050.
For each test case, the first line contains two integers nn and m (1≤n,m≤50)m (1≤n,m≤50)indicating the length of the first two texts (a plaintext and its ciphertext) and the length of the third text which will be given. Each of the second line and the third line contains a string only with capital letters of length nn, indicating a given plaintext and its ciphertext respectively. The fourth line gives another ciphertext only with capital letters of length mm.
We guarantee that the pair of given plaintext (in the second line) and ciphertext (in the third line) is unambiguous with a certain Caesar Cipher.
Output
For each test case, output a line containing Case #x: T, where x is the test case number starting from 11, and T is the plaintext of the ciphertext given in the fourth line.
Example
Input
1 7 7 ACMICPC CEOKERE PKPIZKC
Output
Case #1: NINGXIA
#include <iostream>
#include <cstring>
using namespace std;
char Array[26] = {'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y','Z'};
int main()
{
int T;
scanf("%d",&T);
for(int i=1; i<=T; i++){
int n,m;
cin >> n >> m;
string s1,s2,s3,s;
cin >> s1 >> s2;
int gap = (int)(s2[0] - s1[0]);
cin >> s3;
for(int j=0; j<m; j++){
if(gap >= 0){
int x = (int)(s3[j] - 'A');
int f = (x - gap) % 26;
if(f < 0)
f += 26;
s = s + Array[f];
}else{
int x = (int)(s3[j] - 'A');
int f = (x - gap) % 26;
s = s + Array[f];
}
}
printf("Case #%d: ",i);
cout << s <<endl;
}
return 0;
}