20180717 C. Caesar Cipher

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

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 TB would be replaced by U, and so on.A full exhaustive list is as follows:

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 Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, the first line contains two integers nn and m~(1 \le n,m \le 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 Format

For each test case, output a line containing Case #x: T, where xx is the test case number starting from 11, and TT is the plaintext of the ciphertext given in the fourth line.

样例输入

1
7 7
ACMICPC
CEOKERE
PKPIZKC

样例输出

Case #1: NINGXIA

水题

注意不超范围就行

#include <iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
using namespace std;

int main()
{
    int n;
    int len1,len2;
    int key;
    char a[51],b[51],c[51];
    cin>>n;

    for(int k=1; k<=n; k++)
    {
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
        cin>>len1>>len2;
        cin>>a;
        cin>>b;
        cin>>c;
        key=b[0]-a[0];
        key=-key;
        for(int i=0; i<len2; i++)
        {
            if(c[i]>='a'&&c[i]<='z')
            {
                if(c[i]+key>'z')
                    c[i]=(c[i]-26+key);
                else if(c[i]+key<'a')
                    c[i]=c[i]+key+26;
                else
                    c[i]=c[i]+key;
            }
            else
            {
                if(c[i]+key>'Z')
                    c[i]=(c[i]-26+key);
                else if(c[i]+key<'A')
                    c[i]=c[i]+key+26;
                else
                    c[i]=c[i]+key;
            }
        }
        printf("Case #%d: %s\n",k,c);

    }

    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/z20172123/article/details/81084589
今日推荐