ECNA2016ProblemC:TheKeytoCryptography 水题

Supposeyouneedtoencryptatopsecretmessagelike“SENDMOREMONKEYS". Youcould
use a simple substitution cipher, where each letter in the alphabet is replaced with a different letter.
However,theseciphersareeasilybrokenbyusingthefactthatcertainlettersofthealphabet(like
‘E’,‘S’,and‘A’)appearmorefrequentlythanothers(like‘Q’,‘Z’,and‘X’).Abetterencryption
schemewouldvarythesubstitutionsused foreach letter. Onesuchsystemistheautokeycipher.
Toencryptamessage,youfirstselectasecretword–say“ACM"–andprependittothefrontofthe
th
message. This longer string is truncated to the length of the message and called the key, and the n
th
letter ofthekeyisused toencryptthen letter ofthe originalmessage. Thisencryptionisdone
by treating each letter in the key asa cyclic shift value for the corresponding letter in the message,
where ‘A’ indicates a shift of 0, ‘B’ a shift of 1, and so on. Using “ACM" as the secret word, we
would encryptourmessageasfollows:
SENDMOREMONKEYS (message)
ACMSENDMOREMONK (key)
SGZVQBUQAFRWSLC (ciphertext)
Note that the letter ‘E’ in the message was encrypted as ‘G’ the first time it was encountered (since
thecorresponding letter inthekeywas‘C’indicatinga shiftof2), butthenas‘Q’and‘S’thenext
twotimes.
Yourtaskissimple: givenaciphertextandthesecretword,youmustdeterminetheoriginalmessage.
Input
Input consists of two lines. The first contains the ciphertext and the second contains the secret word.
Bothlinescontainonlyuppercasealphabeticletters.
Output
Displaytheoriginalmessagethatgeneratedthe givenciphertextusingthegivensecretword.
Sample Input 1 Sample Output 1
SGZVQBUQAFRWSLC   SENDMOREMONKEYS

ACM


给出文字加密的方法,然后告诉你密文,让你写出原文  

无非是加加减减的事不详细解释了


ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
    char a[100000];
    char b[100000];
    char c[100000];
    while(gets(a))
    {
      gets(b);
      int lenb=strlen(b);
      int lena=strlen(a);
      if(lenb>=lena)
      {
          for(int i=0;i<lena;i++)
          {
              int x=a[i]-b[i];
              if(x<0)
              c[i]=x+26+'A';
              else
              c[i]=x+'A';
          }
      }
      else
      {
          for(int i=0;i<lenb;i++)
          {
              int x=a[i]-b[i];
              if(x<0)
              c[i]=x+26+'A';
              else
              c[i]=x+'A';
          }
          int j=0;
          for(int i=lenb;i<lena;i++)
          {
            int x=a[i]-c[j];
            if(x<0)
            c[i]=x+26+'A';
            else
            c[i]=x+'A';
            j++;
          }
      }
      cout<<c<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值