题目内容:
一种Playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母),然后与字母表中其他字母一起填入至一个5x5的方阵中,
填入方法如下:
1.首先按行填入密钥串。
2.紧接其后,按字母序按行填入不在密钥串中的字母。
3.由于方阵中只有25个位置,最后剩下的那个字母则不需变换。
如果密钥为youandme,则该方阵如下:
y o u a n
d m e b c
f g h i j
k l p q r
s t v w x
在加密一对字母时,如am,在方阵中找到以这两个字母为顶点的矩形:
o u a
m e b
这对字母的加密字母为该矩形的另一对顶点,如本例中为ob。
请设计程序,使用上述方法对输入串进行加密,并输出加密后的串。
另外有如下规定:
1、一对一对取字母,如果最后只剩下一个字母,则不变换,直接放入加密串中;
2、如果一对字母中的两个字母相同,则不变换,直接放入加密串中;
3、如果一对字母中有一个字母不在正方形中,则不变换,直接放入加密串中;
4、如果字母对出现在方阵中的同一行或同一列,如df或hi,则只需简单对调这两个字母,
即变换为fd或ih;
5、如果在正方形中能够找到以字母对为顶点的矩形,假如字母对为am,则该矩形的另一对顶点字母中,
与a同行的字母应在前面,在上例中应是ob;同样若待变换的字母对为ta,则变换后的字母对应为wo;
6、本程序中输入串均为小写字母,并不含标点、空格或其它字符。
解密方法与加密相同,即对加密后的字符串再加密,将得到原始串。
输入描述
从控制台输入两行字符串,第一行为密钥单词(长度小于等于25,字母不重复,且都为小写字母),
第二行为待加密字符串(长度小于等于50),两行字符串末尾都有一个回车换行符,并且两行字符串均
为小写字母,不含其它字符。
输出描述
在标准输出上输出加密后的字符串。
输入样例
youandme
welcometohangzhou
输出样例
vbrmmomvugnagzguu
暴力美学
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
char a[5][5];
string s1,s2;
void solve(){
int length=s2.length();
int t=length;
s2.c_str();
char s3[length];
if(length%2==1){
s3[length-1]=s2[length-1];
length--;
}
for(int k=0;k<length;k+=2){
pair<int,int> t1(0,0),t2(0,0);
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(a[i][j]==s2[k]){
t1.first=i;
t1.second=j;
}
if(a[i][j]==s2[k+1]){
t2.first=i;
t2.second=j;
}
}
}
if(t1.first==t2.first && t1.second==t2.second){//两个字母相同
s3[k]=s2[k];
s3[k+1]=s2[k+1];
}
else if(t1.first==0 || t2.first ==0 || t1.second==0 ||t2.second==0){//任意一个字母找不到
s3[k]=s2[k];
s3[k+1]=s2[k+1];
}
else if(t1.first==t2.first || t1.second==t2.second){//属于同一列或同一行
s3[k]=s2[k+1];
s3[k+1]=s2[k];
}
else{ //标准情况
s3[k]=a[t1.first][t2.second];
s3[k+1]=a[t2.first][t1.second];
}
}
for(int i=0;i<t;i++){
cout<<s3[i];
}
}
int main(){
bool vocab[27];
cin>>s1;
int length1=s1.length();
cin>>s2;
s1.c_str();
fill(vocab+1,vocab+27,1);
int m=0,n=1;
// cout<<(s1[m]-'a'+1)<<endl;
int t=25-length1;
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(length1>0){
a[i][j]=s1[m];
vocab[s1[m]-'a'+1]=0;
m++;
length1--;
}
else{
while(t>0){
if(vocab[n]==1){
a[i][j]=n+'a'-1;
n++;
t--;
break;
}
n++;
}
}
}
}
solve();
return 0;
}
评判结果
利用clock()函数可得到程序运行时间,clock()使用方法
上述Code运行时间为10ms,而规定时间为3ms! solve()函数的运行时间就有10ms,
AC代码(从11:30到17:52的成果)
#include <iostream>
#include <string>
#include <ctime>
#include <map>
#include <algorithm>
using namespace std;
map<char,int> a;
map<int,char> b;
string s1,s2;
char c[5][5];
clock_t startTime,endTime;
void solve(){
// startTime=clock();
int length=s2.length();
int t=length;
s2.c_str();
char s3[length];
if(length%2==1){
s3[length-1]=s2[length-1];
length--;
}
for(int k=0;k<length;k+=2){
pair<int,int> t1(0,0),t2(0,0);
map<char,int>::iterator iter;
iter=a.find(s2[k]);
if(iter==a.end()){
t1.first=0;t1.second=0;
// cout<<t1.first<<' '<<t1.second<<endl;
}
else{
if((iter->second)%5==0){
t1.first=(iter->second)/5;
t1.second=5;
}
else{
t1.first=(iter->second)/5+1;
t1.second=(iter->second)%5;
}
// cout<<t1.first<<' '<<t1.second<<endl;
}
iter=a.find(s2[k+1]);
if(iter==a.end()){
t2.first=0;t2.second=0;
// cout<<t1.first<<' '<<t1.second<<endl;
}
else{
if((iter->second)%5==0){
t2.first=(iter->second)/5;
t2.second=5;
}
else{
t2.first=(iter->second)/5+1;
t2.second=(iter->second)%5;
}
// cout<<t1.first<<' '<<t1.second<<endl;
}
if(t1.first==t2.first && t1.second==t2.second){//两个字母相同
s3[k]=s2[k];
s3[k+1]=s2[k+1];
}
else if(t1.first==0 || t2.first==0 || t1.second==0 ||t2.second==0){//任意一个字母找不到
s3[k]=s2[k];
s3[k+1]=s2[k+1];
}
else if(t1.first==t2.first || t1.second==t2.second){//属于同一列或同一行
s3[k]=s2[k+1];
s3[k+1]=s2[k];
}
else{ //标准情况
map<int,char>::iterator itera;
itera=b.find((t1.first-1)*5+t2.second);
// cout<<itera->second<<endl;
s3[k]=itera->second;
itera=b.find((t2.first-1)*5+t1.second);
// cout<<itera->second<<endl;
s3[k+1]=itera->second;
}
}
for(int i=0;i<t;i++){
cout<<s3[i];
}
cout<<endl;
// endTime=clock();
}
int main(){
bool vocab[27];
cin>>s1;
int length1=s1.length();
int rs=length1;
cin>>s2;
s1.c_str();
// startTime=clock();
fill(vocab+1,vocab+27,1);
int m=0,n=1;
int t=25-length1;
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(length1>0){
c[i][j]=s1[m];
a[s1[m]]=5*(i-1)+j;
b[5*(i-1)+j]=s1[m];
vocab[s1[m]-'a'+1]=0;
m++;
length1--;
}
else{
while(t>0){
if(vocab[n]==1){
c[i][j]=n+'a'-1;
a[n+'a'-1]=5*(i-1)+j;
b[5*(i-1)+j]=n+'a'-1;
n++;
t--;
break;
}
n++;
}
}
}
}
solve();
// for(int i=1;i<=5;i++){
// for(int j=1;j<=5;j++){
// cout<<c[i][j]<<' ';
// }
// cout<<endl;
// }
// map<char,int>::iterator iter;
// for(iter=a.begin();iter!=a.end();iter++){
// cout<<iter->first<<iter->second<<' ';
// }
// cout<<endl;
// map<int,char>::iterator itert;
// for(itert=b.begin();itert!=b.end();itert++){
// cout<<itert->first<<itert->second<<' ';
// }
// for(int i=1;i<27;i++){
// cout<<i<<' '<<vocab[i]<<endl;
// }
// cout << "The run time is:" <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
// cout << "The run time is:" << (double)clock() /CLOCKS_PER_SEC<< "s" << endl;
return 0;
}