ECNA2016ProblemC:TheKeytoCryptography 水题

来源:互联网 发布:大数据展示平台 编辑:程序博客网 时间:2024/06/05 06:53
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