题目链接

Playfair is a kind of substitution cipher.And the encryption role is simple.In general,there are three steps in Playfair algorithm.

Step 1: Creat a 5*5 secret key table.

Step 2:Tidy plaintext : only leave letters and omit others And if there are capital letters in the plaintext,change it into lower letters.

Step 3: Work out the ciphertext.

Now, let’s see an example.

Firstly,we should creat a 5*5 secret key table.Let’s assume that “ulysses” is the key.If there is a letter appearing more than once we should only retain the first one and delete the others.So we can get the real key “ulyse”.Then we will fill the secret key table with the key “ulyse” and with the other letters which haven’t appeared in the key “ulyse” in order(assume j is equal to i,so every element in the table is unique and j won’t be in this table).Finally, we will get a secret key table as follows:

Secondly,assuming that we will send a important message “balloon”.We should divide ballon into a few groups with exactly two letters.But if the two letters are the same,a single x will be inserted between the same two letters.So “balloon” will be divided into “ba” “lx” “lo” “on”.

Finally,we will encrypt the message.The substitution rules are followed:

1.If the group’s two letters are in the same row in the key table,each letter will be changed into the letter which is just on the right of it.(the first column is seen as on the right of the fifth column).

2.If the group’s two letters are in the same column in the key table,each letter will be changed into the letter which is adjacently below it.(the first row is seen as adjacent below fifth column).

3.If the group’s two letters are neither in the same row nor in the same column in the key table,each letter will be changed into the letter whose position is the same row as itself and the same column as another letter.

So “balloon” will be changed into “cbsvbvpo”.

Ps: j in the plaintext is equal to i and all the letter both in plaintext and ciphertext should be lower letter.And if there are capital letters in the plaintext,change it into lower letters.

Input

The first line will contain the secret key(no more than 25 letters). Then you will get a article(including capital letter and the lower letter).You should omit all the spacing , the punctuation and the numbers(maybe has two or more rows).If there are odd letters after tidying plaintext,delete the last letter.The article would end with a ‘*’.

Ps:You can assume that the article is no more than 10000 letters.And “xx” won’t exist. And the secret key only contains lower letters.

Output

The ciphertext which only includes lower letters.

Sample input and output

Sample Input Sample Output
cipher
balloon
*
dbspgsug
cipher
fill book*
aespslsvqg

Source

第七届ACM趣味程序设计竞赛第四场(正式赛)
 
题解:(见百度百科==)给出一个key,是一个小于25个字母的字符串,然后将这个字符串中的重复字符去掉,横向填入一个5*5的格子里,格子中没填满的用26个字母中没用过的按字典序填入。由于字母有26个,格子是25个,所以字母i和j当做同一个字符处理,均用i表示。然后给出一段包含各种字符的字符串,需要提取其中的字母,字符串的结尾是‘*’。提取出的字母按顺序两两配对,如果两个字母相同,则在中间插入一个‘x’,前一个字母与‘x'配对,后一个字母与该字母的后一字母配对,如果仍相同,也这样处理(不会出现’xx‘这样的数据)。配对后,每两个字母到密码表中匹配,如果同行,则输出相应字母的右边的字母;如果同列,则输出相应字母的下面的字母(第一行为第五行下面,第一列为第五列右边);如果不同行部同列,输出两个字母构成的矩形的另外两个角上的字母(其实就是交换两者的Y坐标并输出)。
自己写的代码一直WA...==b,还没找出错误(结尾给出我的WA代码)...感谢YXT提供的AC代码.
 
#include <iostream>
#include<cstdio>
#include<cstring> using namespace std;
int len1,i,j,k,x1,x2,y1,y2;
int f[],mp[][],a[];
char ch1[]; int xt(char s)
{
if ((int)s>=) return (int)s-;
return (int)s-;
} void solve()
{
int i,j,f[];
len1=strlen(ch1);
j=;
i=;
memset(f,,sizeof(f));
for(int ii=; ii<len1; ii++)
{
int k=xt(ch1[ii]);
if (!f[k])
{
if (k== || k==)
if (f[]) continue;
else k=;
j++;
f[k]=;
mp[i][j]=k;
if (j==)
{
j=;
i++;
}
}
}
for(int ii=; ii<=; ii++)
if (!f[ii])
{
if (ii==) continue;
j++;
mp[i][j]=ii;
if (j==)
{
j=;
i++;
}
}
return;
} void findxy(int *x,int *y,int z)
{
int i,j;
for(i=; i<=; i++)
for(j=; j<=; j++)
if (mp[i][j]!=z) continue;
else
{
*x=i;
*y=j;
return;
}
} int main()
{
freopen("in.txt", "r", stdin);
while(~scanf("%s",&ch1))
{
solve(); memset(a,,sizeof(a));
while (~scanf("%s",&ch1))
{
len1=strlen(ch1);
for(i=; i<len1; i++)
{
if (ch1[i]=='*') goto mm;
if(!(ch1[i]>='a' && ch1[i]<='z' || ch1[i]>='A' && ch1[i]<='Z')) continue;
if (ch1[i]=='j' || ch1[i]=='J') ch1[i]='i';
a[++a[]]=xt(ch1[i]);
}
}
mm:
k=;
while(k<a[])
{
findxy(&x1,&y1,a[k]);
if (a[k+]==a[k]) findxy(&x2,&y2,);
else
{
k++;
findxy(&x2,&y2,a[k]);
} if (x1==x2 && y1!=y2) printf("%c%c",(char)(+mp[x1][y1%+]),(char)(+mp[x2][y2%+]) );
if (x1!=x2 && y1==y2) printf("%c%c",(char)(+mp[x1%+][y1]),(char)(+mp[x2%+][y2]) );
if (x1!=x2 && y1!=y2) printf("%c%c",(char)(+mp[x1][y2]),(char)(+mp[x2][y1]) );
k++;
}
printf("\n");
}
return ;
}
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI 3.1415926
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
#define LOCAL
char mp[][];
int cha[];
string key,line,art;
struct Node
{
char a,b;
};
void mpfill(int idx,char c)//填密码表
{
int i=idx/,j=idx%;
mp[i][j]=c;
return;
}
void mpmake()//生成密码表
{
ms(cha),msp,cha[]=;
int idx=;
string::iterator it;
for(it=key.begin(); it!=key.end(); it++)
{
if(isalpha(*it)==)continue;
*it=tolower(*it);
if(cha[*it-'a']==)
{
mpfill(idx++,*it);
cha[*it-'a']=;
}
}
for(int i=; i<; i++)
{
if(cha[i]==)
{
mpfill(idx++,i+'a');
cha[i]=;
}
}
}
void scanart()
{
line.clear(),art.clear();
while(cin>>line)
{
string::iterator it;
for(it=line.begin(); it!=line.end(); it++)
{
if(*it=='*')return;
if(isalpha(*it))
{
if(tolower(*it)=='j') art.push_back('i');
else art.push_back(tolower(*it));
}
}
}
}
void mpfind(int i1,int j1,int i2,int j2)
{
if(i1==i2)//同行
{
j1=(j1+)%,j2=(j2+)%;
printf("%c%c",mp[i1][j1],mp[i2][j2]);
return;
}
if(j1==j2)//同列
{
i1=(i1+)%,i2=(i2+)%;
printf("%c%c",mp[i1][j1],mp[i2][j2]);
return;
}
//不同行不同列
printf("%c%c",mp[i1][j2],mp[i2][j1]);
}
void scanmp(char c1,char c2)
{
int i1,i2,j1,j2;
for(int i=;i<;i++)
{
int x=i/,y=i%;
if(mp[x][y]==c1)i1=x,j1=y;
if(mp[x][y]==c2)i2=x,j2=y;
}
mpfind(i1,j1,i2,j2);
}
void change()
{
string::iterator it;
for(it=art.begin(); it!=art.end(); it++)
{
Node t;
if(it+==art.end())break;
if(*it!=*(it+))t.a=*it,t.b=*(it+),it++;
else t.a=*it,t.b='x';
scanmp(t.a,t.b);
}
printf("\n");
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
while(cin>>key)
{
mpmake();//密码表
scanart();//读取明文
change();//转换密文
}
return ;
}

CDOJ 1270 Playfair(模拟)的更多相关文章

  1. CDOJ 1270 Playfair

    模拟题,代码写得比较乱... #include<cstdio> #include<cstring> #include<cmath> #include<queu ...

  2. cdoj 25 点球大战(penalty) 模拟题

    点球大战(penalty) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/2 ...

  3. CDOJ 3 BiliBili, ACFun… And More! 模拟

    原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...

  4. 模拟一下goldengate中断后,重新同步操作

    模拟一下goldengata中断后,重新同步操作:     1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program     Status    ...

  5. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  6. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  7. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  8. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

  9. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

随机推荐

  1. android 沉浸式状态栏的实现

    本文介绍一种简单的实现沉浸式状态栏的方法,要高于或等于api19才可以. 实现android沉浸式状态栏很简单,添加代码两步就可以搞定. 一.在activity中添加 getWindow().addF ...

  2. Android 不能返回 parent Activity 的问题

    使用 ActionBar,开启返回按钮: 在 Activity 的 onCreate 中添加下面代码 getSupportActionBar().setDisplayHomeAsUpEnabled(t ...

  3. 详细介绍Java垃圾回收机制

    垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...

  4. 等比缩放之自适应神器——css3的rem

    1.rem简介   rem是CSS3中新增加的一个单位值,他和em单位一样,都是一个相对单位.不同的是em是相对于元素的父元素的font-size进行计算:rem是相对于根元素html的font-si ...

  5. CentOS 6.5静态IP的设置(NAT和桥接都适用)

    CentOS 6.5静态IP的设置(NAT和桥接都适用) 为了方便,用Xshell来.并将IP设置为静态的.因为,在CentOS里,若不对其IP进行静态设置的话,则每次开机,其IP都是动态变化的,这样 ...

  6. JDBC:四步完成MySQL数据库的连接

    ->首先,将MySQL的jar包引入 ->然后创建一个.properties的文件(例:connection.properties),在该文件中写入如下代码: jdbc.driver.cl ...

  7. Redis 的几种数据结构&五种数据类型对象

    先看几种数据结构 通过分析底层的数据结构,学习如何根据场景选型和设计 1,简单动态字符串 redis使用的字符串SDS有别于C语言中的字符串 a, 结构 free字段为已分配但未使用的空间 len为已 ...

  8. 如何使用Maven的archetype快速生成一个新项目(解决生成项目目录不完整问题)

    Maven的archetype Plugin可能大家都听过,但不一定都能很好地用好它.缺省地如果你使用 mvn archetype:generate  会从maven的Repository里查找所有支 ...

  9. Objective-C Runtime 运行时之二:成员变量与属性(转载)

    在前面一篇文章中,我们介绍了Runtime中与类和对象相关的内容,从这章开始,我们将讨论类实现细节相关的内容,主要包括类中成员变量,属性,方法,协议与分类的实现. 本章的主要内容将聚集在Runtime ...

  10. unity3D中重要函数

    Update 当MonoBehaviour启用时,其Update在每一帧被调用. LateUpdate 当Behaviour启用时,其 LateUpdate在每一帧被调用. FixedUpdate 当 ...