在这个有话不直说的年代,密码学越来越被广泛接受。我们引用经典的“凯撒密码”。在英文中,凯撒加密只对26个字母生效(分大小写)我们按照a到z来排字母。凯撒加密的原理就是把原文的每一个字母都按顺序往后移K位。这个K将被作为密钥。(’a’往后移变成’b’,’z’往后移会变成’a’) (0< = K< = 25)现在给出一系列用凯撒加密的英文句子,请你编写程序逐句翻译。也就是说,请你确定一个密钥,使得解码以后的文字最符合英文的规则与规范。数据保证存在唯一的解码方案,使得明码是完全可以分辨的英文句子。
在这个有话不直说的年代,密码学越来越被广泛接受。我们引用经典的“凯撒密码”。在英文中,凯撒加密只对26个字母生效(分大小写)我们按照a到z来排字母。凯撒加密的原理就是把原文的每一个字母都按顺序往后移K位。这个K将被作为密钥。(’a’往后移变成’b’,’z’往后移会变成’a’) (0< = K< = 25)现在给出一系列用凯撒加密的英文句子,请你编写程序逐句翻译。也就是说,请你确定一个密钥,使得解码以后的文字最符合英文的规则与规范。数据保证存在唯一的解码方案,使得明码是完全可以分辨的英文句子。
输入一定包括10行每一行都是用同一密钥加密的英文。
输出10行,为解密结果。不允许格式上有任何不同。
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 std::string tmp,dict[700]={"a","i","am","an","as","at","be","by","do","go","he","if","in","is","it","ll","me","mr","my","no","of","on","or","re","so","to","up","ve","we","ago","all","and","any","are","bar","bat","big","bit","boy","bug","but","can","cat","cup","cut","day","did","dog","don","end","fee","fly","for","fox","fur","get","gnu","god","gpl","had","has","her","him","his","how","iee","iel","its","joy","key","law","let","may","mrs","new","nor","not","now","one","our","out","paw","pay","ran","run","sat","saw","see","set","she","six","tea","the","too","two","use","war","was","way","who","why","yet","you","zoo","adam","also","aren","away","back","bank","bear","beat","beef","been","bell","bill","bird","boar","body","bone","book","bore","both","came","camp","case","code","come","cook","copy","damn","damp","date","dead","deer","deny","dish","does","dull","each","ever","eyes","face","felt","fill","find","fire","fish","five","four","free","from","gain","gave","gets","girl","give","glad","good","hand","hard","have","here","high","hold","hour","hunt","ieee","into","jack","just","keep","kept","kiss","knew","know","lamb","last","lazy","lead","left","lend","life","like","live","long","look","love","made","make","many","mars","mary","meat","meet","mind","mine","moon","moor","more","morn","most","much","must","name","need","nine","once","only","over","paid","pain","papa","pass","past","pays","pink","play","poor","pray","puma","rain","ramp","rope","said","sake","same","seen","shed","show","sing","some","song","stop","such","sure","take","tape","tell","than","that","them","then","they","this","tied","time","tiny","told","tony","took","tree","trip","true","upon","used","user","very","wall","want","wasn","ways","well","went","were","what","when","whom","wife","wild","will","wish","with","wood","work","year","your","about","added","adore","after","again","aleko","along","among","apple","apply","armed","asked","began","being","below","betty","black","blade","bleed","blood","bored","brown","carry","catch","cause","chuck","clock","comes","could","death","doesn","doing","dwelt","eight","every","facts","fetch","field","fifty","first","forty","found","fully","given","gives","going","grant","great","hands","happy","hares","heart","hours","human","jumps","keeps","kinds","known","lamps","large","legal","lines","lived","local","lover","lovin","madam","makes","marks","merry","miles","money","moses","names","never","night","offer","often","other","paint","parts","place","plain","plays","price","prize","prove","pumas","quick","quiet","reach","right","roses","scamp","seven","shall","share","shine","sight","since","sixty","small","sorry","south","speak","stars","start","steel","steps","still","sword","table","taken","teeth","terms","thank","theft","their","there","these","thief","thing","think","those","three","times","today","torch","tower","trade","trail","truly","trust","under","users","vicar","wants","watch","weeks","where","which","while","white","whole","wings","woken","woman","words","works","world","worth","would","yards","years","yours"}; 5 char s[1000],s0[1000],s1[30];int ii=10,i,j,k,l,ans,now,key; 6 int inmap(){ 7 for(int i=0;i<441;i++)if(tmp==dict[i])return 1; 8 return 0; 9 } 10 int check(int key){ 11 int f=0;memset(s0,0,sizeof(s0)); 12 for(j=0;s[j];j++) 13 if(s[j]>=‘A‘&&s[j]<=‘Z‘)s0[j]=s[j]-‘A‘+‘a‘; 14 else s0[j]=s[j]; 15 for(j=0;s0[j];j++)if(s0[j]>=‘a‘&&s0[j]<=‘z‘)s0[j]=(s0[j]-‘a‘+key)%26+‘a‘; 16 for(j=0;s0[j];j++) 17 if(s0[j]>=‘a‘&&s0[j]<=‘z‘){ 18 for(tmp="",k=j,l=0;s0[k]&&s0[k]>=‘a‘&&s0[k]<=‘z‘;k++)tmp+=s0[k]; 19 if(inmap())f++;j=k-1; 20 } 21 return f; 22 } 23 int main(){ 24 //freopen("sh.txt","r",stdin); 25 for(int k;gets(s);puts(s)){ 26 for(ans=0,k=0;k<26;k++)if((now=check(k))>ans)ans=now,key=k; 27 for(i=0;s[i];i++) 28 if(s[i]>=‘a‘&&s[i]<=‘z‘)s[i]=(s[i]-‘a‘+key)%26+‘a‘; 29 else if(s[i]>=‘A‘&&s[i]<=‘Z‘)s[i]=(s[i]-‘A‘+key)%26+‘A‘; 30 } 31 }
原文:http://www.cnblogs.com/shenben/p/5459943.html