ZOJ 1009 Enigma
2016-01-30 17:59
351 查看
In World War II, Germany once used an electronic encryption machine called Enigma, which played a decisive role in the initial victories of Nazi Germany. It was proved to be one of the most reliable encryption systems in history. However, it was the blind
trust on the reliability of the machine that brought about the doom of its user.
The structure of a one-rotor Enigma is shown as follows (the Enigma has only six keys):
The key element of the Enigma is the rotor, as shown in the second figure, which uses electronic circuits to transform plaintext (input from keyboard) into cryptograph (output on screen). When one key on the keyboard is pressed, the corresponding cryptograph
is shown on screen. Then the rotor will automatically revolve a one-letter-step to a different position. The following figures illustrate how the rotor works when letter "b" is pressed three successively times:
When letter "b" is pressed for the first time, the signal goes through the circuit and "A" is shown on screen. When the key is released, the rotor revolves one-letter-step to a different position that changes all the corresponding circuits so that each letter
now has a different cryptograph. When letter "b" is pressed for the second time, the corresponding cryptograph is "C". So when letter "b" is pressed for the third time, the cryptograph is "E" according to the principle specified above.
Now the following figure shows the structure of a two-rotor Enigma.
The difference is that when a key is released, the second rotor won't revolve a step until the first one has finished one circle and returns to the original position. This is also the same in the case of three-rotor Enigma. That is: Only after the first
rotor has finished one circle and return to the initial status, the second rotor will revolve a step. And only after the second rotor has finish one circle, the third rotor will revolve a step.
However, how did the Allied Forces obtain the information encrypted by Enigma? A person named Hans-Thilo Schimdt was very essential. He acted as a spy and provided the initial status of the three rotors in each Enigma to the Allied Forces once a month. The
Allied Forces thus got everything they wanted by deciphering the intercepted cryptograph using the information offered by the spy.
Now, please design a program to obtain the plaintexts using the information offered by the Allied Forces.
Input
The input file contains several test cases representing several three-rotor Enigmas. The last test case in the input file is followed by a line containing a number 0.
Each case begins with a line containing an integer m (1 <= m <= 26) which indicates the number of sequential letters each rotor has. The first letter will always be A. (for example, m = 6 tells each rotor has 6 keys from A to F). The following three lines describe
the initial status of the three rotors respectively. Each of them contains a string consisting of m capital character. For instance, a rotor with the initial status "BADFEC" indicates that the initial encrypt mechanism is to convert "abcdef" to "BADFEC", that
is, original letter "a" corresponding to cryptograph letter "B", "b" to "A", "c" to "D", "d" to "F", "e" to "E" and "f" to "C". The forth line of each case contains an integer n which tells the number of cryptographs generated by the above Enigma. Then the
following n lines are the n cryptographs respectively, which consist of m capital characters each.
Output
For each test case, the output should consist of two parts. The first line is the number of Enigma and a colon. The following lines are the plaintexts deciphered from the corresponding cryptographs. Each plaintext should be printed in one line. Note: The characters
in the plaintext should be converted to the corresponding lowercases before they are printed.
Insert a blank line between test cases.
Sample Input
6
BADFEC
ABCDEF
ABCDEF
1
ACE
0
Output for the Sample Input
Enigma 1:
bbb
代码如下:
T^T *……*
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string s[3];
int rotor[3][60];
string model="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int co1,co2,co3;
void change( int n )
{
int i,j,t,temp[3][60];
for( i=0;i<3;i++ )
{
for( j=0;j<n;j++ )
{
t=j+rotor[i][j];
temp[i][t]=-rotor[i][j];
temp[i][t]=( temp[i][t]+n )%n ;
}
}
for( i=0;i<3;i++ )
{
for( j=0;j<n;j++ )
rotor[i][j+n]=rotor[i][j]=temp[i][j];
}
}
void output( char c,int n )
{
int i,t,sta[3];
sta[2]=n-co3;
sta[1]=n-co2;
sta[0]=n-co1;
/* for( i=2;i>=0;i-- )
cout<<sta[i];
cout<<endl;*/
for( i=2;i>=0;i-- )
{
t=rotor[i][ sta[i]+c-'A' ];
c='A'+( c-'A'+t )%n;
}
co1++;
if( co1==n )
{
co2++;
co1=0;
if( co2==n )
{
co3++;
co2=0;
if( co3==n )
co3=0;
}
}
cout<<char( c-('A'-'a') );
}
int main()
{
int n,m,i,j,first,count;
string temp;
first=1;
while( cin>>n && n )
{
for( i=0;i<3;i++ )
{
cin>>s[i];
for( j=0;j<n;j++ )
rotor[i][j]=s[i][j]-model[j];
}
change( n );
cin>>m;
if( first!=1 ) cout<<endl;
cout<<"Enigma "<<first<<":\n";
while( m-- )
{
cin>>temp;
co1=co2=co3=0;
for( i=0;i<temp.size();i++ )
{
output( temp[i],n );
}
cout<<endl;
}
first++;
}
return 0;
}
trust on the reliability of the machine that brought about the doom of its user.
The structure of a one-rotor Enigma is shown as follows (the Enigma has only six keys):
The key element of the Enigma is the rotor, as shown in the second figure, which uses electronic circuits to transform plaintext (input from keyboard) into cryptograph (output on screen). When one key on the keyboard is pressed, the corresponding cryptograph
is shown on screen. Then the rotor will automatically revolve a one-letter-step to a different position. The following figures illustrate how the rotor works when letter "b" is pressed three successively times:
When letter "b" is pressed for the first time, the signal goes through the circuit and "A" is shown on screen. When the key is released, the rotor revolves one-letter-step to a different position that changes all the corresponding circuits so that each letter
now has a different cryptograph. When letter "b" is pressed for the second time, the corresponding cryptograph is "C". So when letter "b" is pressed for the third time, the cryptograph is "E" according to the principle specified above.
Now the following figure shows the structure of a two-rotor Enigma.
The difference is that when a key is released, the second rotor won't revolve a step until the first one has finished one circle and returns to the original position. This is also the same in the case of three-rotor Enigma. That is: Only after the first
rotor has finished one circle and return to the initial status, the second rotor will revolve a step. And only after the second rotor has finish one circle, the third rotor will revolve a step.
However, how did the Allied Forces obtain the information encrypted by Enigma? A person named Hans-Thilo Schimdt was very essential. He acted as a spy and provided the initial status of the three rotors in each Enigma to the Allied Forces once a month. The
Allied Forces thus got everything they wanted by deciphering the intercepted cryptograph using the information offered by the spy.
Now, please design a program to obtain the plaintexts using the information offered by the Allied Forces.
Input
The input file contains several test cases representing several three-rotor Enigmas. The last test case in the input file is followed by a line containing a number 0.
Each case begins with a line containing an integer m (1 <= m <= 26) which indicates the number of sequential letters each rotor has. The first letter will always be A. (for example, m = 6 tells each rotor has 6 keys from A to F). The following three lines describe
the initial status of the three rotors respectively. Each of them contains a string consisting of m capital character. For instance, a rotor with the initial status "BADFEC" indicates that the initial encrypt mechanism is to convert "abcdef" to "BADFEC", that
is, original letter "a" corresponding to cryptograph letter "B", "b" to "A", "c" to "D", "d" to "F", "e" to "E" and "f" to "C". The forth line of each case contains an integer n which tells the number of cryptographs generated by the above Enigma. Then the
following n lines are the n cryptographs respectively, which consist of m capital characters each.
Output
For each test case, the output should consist of two parts. The first line is the number of Enigma and a colon. The following lines are the plaintexts deciphered from the corresponding cryptographs. Each plaintext should be printed in one line. Note: The characters
in the plaintext should be converted to the corresponding lowercases before they are printed.
Insert a blank line between test cases.
Sample Input
6
BADFEC
ABCDEF
ABCDEF
1
ACE
0
Output for the Sample Input
Enigma 1:
bbb
代码如下:
T^T *……*
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string s[3];
int rotor[3][60];
string model="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int co1,co2,co3;
void change( int n )
{
int i,j,t,temp[3][60];
for( i=0;i<3;i++ )
{
for( j=0;j<n;j++ )
{
t=j+rotor[i][j];
temp[i][t]=-rotor[i][j];
temp[i][t]=( temp[i][t]+n )%n ;
}
}
for( i=0;i<3;i++ )
{
for( j=0;j<n;j++ )
rotor[i][j+n]=rotor[i][j]=temp[i][j];
}
}
void output( char c,int n )
{
int i,t,sta[3];
sta[2]=n-co3;
sta[1]=n-co2;
sta[0]=n-co1;
/* for( i=2;i>=0;i-- )
cout<<sta[i];
cout<<endl;*/
for( i=2;i>=0;i-- )
{
t=rotor[i][ sta[i]+c-'A' ];
c='A'+( c-'A'+t )%n;
}
co1++;
if( co1==n )
{
co2++;
co1=0;
if( co2==n )
{
co3++;
co2=0;
if( co3==n )
co3=0;
}
}
cout<<char( c-('A'-'a') );
}
int main()
{
int n,m,i,j,first,count;
string temp;
first=1;
while( cin>>n && n )
{
for( i=0;i<3;i++ )
{
cin>>s[i];
for( j=0;j<n;j++ )
rotor[i][j]=s[i][j]-model[j];
}
change( n );
cin>>m;
if( first!=1 ) cout<<endl;
cout<<"Enigma "<<first<<":\n";
while( m-- )
{
cin>>temp;
co1=co2=co3=0;
for( i=0;i<temp.size();i++ )
{
output( temp[i],n );
}
cout<<endl;
}
first++;
}
return 0;
}
相关文章推荐
- Qualcomm平台qcril初始化及消息处理流程(原)
- ios绘图demo,做一个涂鸦板(下)
- iOS之UI学习开篇
- JUI 分页代码
- 如何完全的卸载MySQL数据库
- java-迭代删除
- mysql之show engine innodb status解读
- 内存实例分析
- NIO--JAVA NIO之浅谈内存映射文件原理与DirectMemory
- 机器字节序测试
- 月光微博客
- js常用框架原理
- ios绘图demo,做一个涂鸦板(上)
- LeetCode刷题 (Python) | 155. Min Stack
- 4 基于概率论的分类方法:朴素贝叶斯(二)
- 日期2
- DJANGO之分页
- 海康大华客户端开发
- ios绘图基础
- A-MSDU 和 AMPDU