题目描述
见题目
样例
Input:
CompleteVictory
Yvqgpxaimmklongnzfwpvxmniytm
Output:
Wherethereisawillthereisaway
算法
模拟
详见代码中注释!
C++ 代码
#include<bits/stdc++.h>
using namespace std;
string tab[26]={
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"BCDEFGHIJKLMNOPQRSTUVWXYZA",
"CDEFGHIJKLMNOPQRSTUVWXYZAB",
"DEFGHIJKLMNOPQRSTUVWXYZABC",
"EFGHIJKLMNOPQRSTUVWXYZABCD",
"FGHIJKLMNOPQRSTUVWXYZABCDE",
"GHIJKLMNOPQRSTUVWXYZABCDEF",
"HIJKLMNOPQRSTUVWXYZABCDEFG",
"IJKLMNOPQRSTUVWXYZABCDEFGH",
"JKLMNOPQRSTUVWXYZABCDEFGHI",
"KLMNOPQRSTUVWXYZABCDEFGHIJ",
"LMNOPQRSTUVWXYZABCDEFGHIJK",
"MNOPQRSTUVWXYZABCDEFGHIJKL",
"NOPQRSTUVWXYZABCDEFGHIJKLM",
"OPQRSTUVWXYZABCDEFGHIJKLMN",
"PQRSTUVWXYZABCDEFGHIJKLMNO",
"QRSTUVWXYZABCDEFGHIJKLMNOP",
"RSTUVWXYZABCDEFGHIJKLMNOPQ",
"STUVWXYZABCDEFGHIJKLMNOPQR",
"TUVWXYZABCDEFGHIJKLMNOPQRS",
"UVWXYZABCDEFGHIJKLMNOPQRST",
"VWXYZABCDEFGHIJKLMNOPQRSTU",
"WXYZABCDEFGHIJKLMNOPQRSTUV",
"XYZABCDEFGHIJKLMNOPQRSTUVW",
"YZABCDEFGHIJKLMNOPQRSTUVWX",
"ZABCDEFGHIJKLMNOPQRSTUVWXY"};//密钥表
string k,m;//k为密钥,m为密文
bool upp[1001];
int main()
{
cin>>k>>m;
for(int i=0;i<k.size();i++)//将密钥转成大写
if(k[i]>='a' && k[i]<='z') k[i]-=32;
//如果密钥长度短于密文长度,将密钥串循环补至大于密文长度
string s=k;
while(k.size()<m.size()) k+=s;
for(int i=0;i<m.size();i++)//将密文转成大写,并记录原大写位置
if(m[i]>='a' && m[i]<='z') m[i]-=32;
else upp[i]=true;
for(int i=0;i<m.size();i++)
{
int x=m[i]-64,y=-1;//x为当前密文所有行,从(x,0)位置开始向右上方查找
while(true)
{
x--;//右上x递减
y++;//右上y递增
if(x<0) x=25;//如果x越界,则至末行
if(y>25) y=0;//如果y越界,则至首列
if(y==(k[i]-65))//找到密钥所在列,则行即为所求
{
//若此位原为大写,还按大写输出行号所代表字符,否则按小写输出
if(upp[i]) cout<<(char)(x+65);
else cout<<(char)(x+65+32);
break;
}
}
}
return 0;
}
太赞👍了