您的位置:首页 > Web前端

[Offer收割]编程练习赛6 题目1 : Playfair密码表 (密码学——模拟)

2016-08-21 16:26 471 查看
传送门

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

小Hi和小Ho经常用Playfair密码表加密自己的代码。 密码表是按以下步骤生成的。

随机选择一个只包含大写字母的单词S作为密钥。

将S中的所有字母J替换为字母I。

将S中的字母依次填写进一个5x5的矩阵,按照从上到下、从左到右的顺序填充格子。填充过程中略过已经在密码表中的字母。

将’A’-‘I’, ‘K’-‘Z’(除去J之外的所有大写字母)中没有出现在密码表中的大写字母按照字母表顺序填入矩阵剩余的格子中。

举个例子:单词DIJSTRA,替换字母得到DIISTRA;将DIISTRA填入矩阵得到的密码表为(注意第二个I被略过了):

DISTR
A....
.....
.....
.....


最后将剩余字母填入,得到密码表:

DISTR
ABCEF
GHKLM
NOPQU
VWXYZ


给定作为密钥的单词,你能求出密码表吗?

输入

第1行:一行字符串,只包含大写字母,长度不超过200

输出

共5行,每行5个字母,表示密码表。

样例输入

HIHOCODER


样例输出

HIOCD
ERABF
GKLMN
PQSTU
VWXYZ


解题思路:

模拟。。。

/**
2016 - 08 - 19 下午
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e2+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
LL res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
int Scan_Int()///输入外挂
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Out(LL a)///输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
char f[MAXN];
int vis[MAXN];
char str[MAXN][MAXN];
int main()
{
while(cin>>f)
{
memset(vis,0,sizeof(vis));
int len = strlen(f);
for(int i=0; i<len; i++)
{
if(f[i] == 'J')
f[i] = 'I';
}
int tot = 0,k = 0;
int x, y;
///得到密码矩阵
for(int i=0; i<len; i++)
{
if(!vis[f[i]-'A'+1])
{
x = k/5;
y = k%5;
str[x][y] = f[i];
vis[f[i]-'A'+1] = 1;
k++;
}
}
for(int i=1; i<=26; i++)
{
if(!vis[i] && i!=10)
{
x = k/5;
y = k%5;
str[x][y] = i+'A'-1;
vis[i] = 1;
k++;
}
}
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<str[i][j];
}
puts("");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: