HNU2019暑期训练赛之2 K. 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 a solution: the famous “Autokey” cipher. He uses a version that takes the 26 lower-case letters ‘a’–‘z’ and internally translates them in alphabetical order to the numbers 0 to 25.

The encryption key k begins with a secret prefix of n letters. Each of the remaining letters of the key is copied from the letters of the plaintext aaa, so that kn+i=aik_{n+i} = a_ikn+i=ai for i ≥ 1. Encryption of the plaintext a to the ciphertext b follows the formula bi=ai+kib_i = a_i + k_ibi=ai+ki mod 26.

Mary is not easily discouraged. She was able to get a peek at the last n letters John typed into his diary on the family computer before he noticed her, quickly encrypted the text document with a click, and left. This could be her chance.

Input

The input consists of:
• One line with two integers nnn and m(1≤n≤30,n+1≤m≤100)m (1 ≤ n ≤ 30, n + 1 ≤ m ≤ 100)m(1n30,n+1m100), where nnn is the length of the keyword as well as the number of letters Mary saw, and m is the length of the text.
• One line with nnn lower-case letters, the last nnn letters of the plaintext.
• One line with mmm lower-case letters, the whole ciphertext.

Output

Output the plaintext of John’s diary.

Sample Input

5 16
again
pirpumsemoystoal

Sample Output

marywasnosyagain

Sample Input2

1 12
d
fzvfkdocukfu

Sample Output2

shortkeyword

AC Code

#include <iostream>
#include<cstdio>
using namespace std;
char key[150];
char b[150];
char a[150];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, m;
    cin >> n >> m;
    cin >> (key+m-n);
    char tempKey[100];
    cin >> b;
    for(int j = m-1; j>=n; --j){
        a[j] = b[j] - key[j] +'a';
        if (a[j] < 'a')
            a[j] += 26;
        key[j-n] = a[j];
    }
    cout <<(key)<<endl;
    cout << a;
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值