洛谷 P1079 Vigenère 密码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷 P1079 Vigenère 密码相关的知识,希望对你有一定的参考价值。
题目描述
16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密
码。Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为
南军所广泛使用。
在密码学中,我们称需要加密的信息为明文,用 M 表示;称加密后的信息为密文,用
C 表示;而密钥是一种参数,是将明文转换为密文或将密文转换为明文的算法中输入的数据,
记为 k。 在 Vigenère 密码中,密钥 k 是一个字母串,k=k1k2…kn。当明文 M=m1m2…mn时,
得到的密文 C=c1c2…cn,其中 ci=mi®ki,运算®的规则如下表所示:
Vigenère 加密在操作时需要注意:
-
®运算忽略参与运算的字母的大小写,并保持字母在明文 M 中的大小写形式;
- 当明文 M 的长度大于密钥 k 的长度时,将密钥 k 重复使用。
例如,明文 M=Helloworld,密钥 k=abc 时,密文 C=Hfnlpyosnd。
输入输出格式
输入格式:
输入共 2 行。
第一行为一个字符串,表示密钥 k,长度不超过 100,其中仅包含大小写字母。第二行
为一个字符串,表示经加密后的密文,长度不超过 1000,其中仅包含大小写字母。
输出格式:
输出共 1 行,一个字符串,表示输入密钥和密文所对应的明文。
输入输出样例
CompleteVictory Yvqgpxaimmklongnzfwpvxmniytm
Wherethereisawillthereisaway
说明
【数据说明】
对于 100%的数据,输入的密钥的长度不超过 100,输入的密文的长度不超过 1000,且
都仅包含英文字母。
NOIP 2012 提高组 第一天 第一题
模拟
#include <cstring> #include <cstdio> #define N 1005 #define rep(a,b,c) for(int a=b;a<=c;++a) int jm[45][45]= {{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0}, {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1}, {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2}, {4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3}, {5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4}, {6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5}, {7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6}, {8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7}, {9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8}, {10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9}, {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10}, {12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11}, {13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12}, {14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13}, {15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}, {16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, {17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, {18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}, {19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}, {20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}, {21,22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, {22,23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}, {23,24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22}, {24,25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23}, {25,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}}; char k[N],s[N]; int main() { scanf("%s",k); scanf("%s",s); int lk=strlen(k),ls=strlen(s),j=0; rep(i,0,ls-1) { int p=k[j]-‘A‘,g=s[i]-‘A‘,f=0; if(p>25) p-=32; if(g>25) g-=32,f=1; rep(k,0,25) if(jm[p][k]==g) f?putchar(k+‘A‘+32):putchar(k+‘A‘); j=(j+1)%lk; } return 0; }
以上是关于洛谷 P1079 Vigenère 密码的主要内容,如果未能解决你的问题,请参考以下文章
[NOIP2012] 提高组 洛谷P1079 Vigenère 密码
luoguP1079 Vigenère 密码 题解(NOIP2012)