ZOJ 1009 Enigma
2016-03-14 21:04
483 查看
Enigma
Time Limit: 10 Seconds Memory Limit: 32768 KB
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
误解题意了,浪费了很多时间。题意是第一个rotor每转完一圈的那下,第二个rotor同时转一下,并不是先后顺序。
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <stdio.h>
using namespace std;
class rotor{
public:
string initial_status, status;
int *proj, size, n;
rotor *next;
rotor (int input_n): size(0), n(input_n) {}
void init(string s){
transform(s.begin(), s.end(), s.begin(), ::tolower);
initial_status = s;
status = s;
proj = new int[s.size()];
int t;
for(string::iterator iter = s.begin(); iter != s.end(); iter++){
t = *iter - 'a' - size;
while(t < 0)
t += n;
t = t % n;
proj[size++] = t;
}
}
void rotate(){
int temp = proj[size - 1];
for(int i = size - 1; i > 0; i--){
proj[i] = proj[i - 1];
}
proj[0] = temp;
string t;
for(int i = 0; i < size; i++)
t += 'a' + (proj[i] + i) % n;
status = t;
// cout<<"after rotated: "<<status<<endl;
}
char input(char enig){
// cout<<enig<<' ';
// for(int i = 0; i < size; i++)
// cout<<proj[i]<<' ';
// cout<<status<<' '<<endl;
int t = enig - proj[status.find(enig)] - 'a';
while(t < 0)
t += n;
t = t % n;
char crypt = t + 'a';
// cout<<crypt<<endl;
return crypt;
}
string print(){
string t;
for(int i = 0; i < size; i++)
t += 'a' + (proj[i] + i) % n;
return t;
}
};
class three_rotor{
public:
rotor *r1, *r2, *r3;
int size;
three_rotor(rotor *rotor1, rotor *rotor2, rotor *rotor3, int s): r1(rotor1), r2(rotor2), r3(rotor3), size(s){
r1->next = r2;
r2->next = r3;
r3->next = r1;
}
char input(char enig){
return r1->input(r2->input(r3->input(enig)));
}
void print(){
cout<<r3->next->print()<<endl<<r1->next->print()<<endl<<r2->next->print()<<endl;
}
void reset(){
r1->size = 0;
r1->init(r1->initial_status);
r2->size = 0;
r2->init(r2->initial_status);
r3->size = 0;
r3->init(r3->initial_status);
}
void update(int r){
r1->rotate();
if(r % (size * size) == 0)
r3->rotate();
if(r % size == 0)
r2->rotate();
}
};
int main(){
int n, m, rotate_times, n_case = 1;
string in;
rotor *r[3];
while( cin>>m && m ){
for(int i = 0; i < 3; i++){
cin>>in;
r[i] = new rotor(m);
r[i]->init(in);
}
three_rotor throtor(r[0], r[1], r[2], m);
cin>>n;
if(n_case > 1) cout<<endl;
cout<<"Enigma "<<n_case++<<":"<<endl;
for(int i = 1; i <= n; i++){
rotate_times = 1;
cin>>in;
transform(in.begin(), in.end(), in.begin(), ::tolower);
for(string::iterator iter = in.begin(); iter != in.end(); iter++){
// throtor.print();
// cout<<"rotate_times: "<<rotate_times<<endl;
cout<<throtor.input(*iter);
throtor.update(rotate_times);
rotate_times ++;
}
throtor.reset();
cout<<endl;
}
}
return 0;
}
Time Limit: 10 Seconds Memory Limit: 32768 KB
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
误解题意了,浪费了很多时间。题意是第一个rotor每转完一圈的那下,第二个rotor同时转一下,并不是先后顺序。
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <stdio.h>
using namespace std;
class rotor{
public:
string initial_status, status;
int *proj, size, n;
rotor *next;
rotor (int input_n): size(0), n(input_n) {}
void init(string s){
transform(s.begin(), s.end(), s.begin(), ::tolower);
initial_status = s;
status = s;
proj = new int[s.size()];
int t;
for(string::iterator iter = s.begin(); iter != s.end(); iter++){
t = *iter - 'a' - size;
while(t < 0)
t += n;
t = t % n;
proj[size++] = t;
}
}
void rotate(){
int temp = proj[size - 1];
for(int i = size - 1; i > 0; i--){
proj[i] = proj[i - 1];
}
proj[0] = temp;
string t;
for(int i = 0; i < size; i++)
t += 'a' + (proj[i] + i) % n;
status = t;
// cout<<"after rotated: "<<status<<endl;
}
char input(char enig){
// cout<<enig<<' ';
// for(int i = 0; i < size; i++)
// cout<<proj[i]<<' ';
// cout<<status<<' '<<endl;
int t = enig - proj[status.find(enig)] - 'a';
while(t < 0)
t += n;
t = t % n;
char crypt = t + 'a';
// cout<<crypt<<endl;
return crypt;
}
string print(){
string t;
for(int i = 0; i < size; i++)
t += 'a' + (proj[i] + i) % n;
return t;
}
};
class three_rotor{
public:
rotor *r1, *r2, *r3;
int size;
three_rotor(rotor *rotor1, rotor *rotor2, rotor *rotor3, int s): r1(rotor1), r2(rotor2), r3(rotor3), size(s){
r1->next = r2;
r2->next = r3;
r3->next = r1;
}
char input(char enig){
return r1->input(r2->input(r3->input(enig)));
}
void print(){
cout<<r3->next->print()<<endl<<r1->next->print()<<endl<<r2->next->print()<<endl;
}
void reset(){
r1->size = 0;
r1->init(r1->initial_status);
r2->size = 0;
r2->init(r2->initial_status);
r3->size = 0;
r3->init(r3->initial_status);
}
void update(int r){
r1->rotate();
if(r % (size * size) == 0)
r3->rotate();
if(r % size == 0)
r2->rotate();
}
};
int main(){
int n, m, rotate_times, n_case = 1;
string in;
rotor *r[3];
while( cin>>m && m ){
for(int i = 0; i < 3; i++){
cin>>in;
r[i] = new rotor(m);
r[i]->init(in);
}
three_rotor throtor(r[0], r[1], r[2], m);
cin>>n;
if(n_case > 1) cout<<endl;
cout<<"Enigma "<<n_case++<<":"<<endl;
for(int i = 1; i <= n; i++){
rotate_times = 1;
cin>>in;
transform(in.begin(), in.end(), in.begin(), ::tolower);
for(string::iterator iter = in.begin(); iter != in.end(); iter++){
// throtor.print();
// cout<<"rotate_times: "<<rotate_times<<endl;
cout<<throtor.input(*iter);
throtor.update(rotate_times);
rotate_times ++;
}
throtor.reset();
cout<<endl;
}
}
return 0;
}
相关文章推荐
- 数列转换
- 论background-image和img的区别
- 装饰模式
- 关于字符串匹配计数和编程思想方法
- 本博阅读指南
- 《Linux内核设计与实现》读书笔记 第二章 从内核出发
- 蓝桥杯 历届试题 7对数字 直角三角形
- C 语言基础(三)
- PythonChallenge Mission 8
- tomcat session redis支持
- iOS开发ARC内存管理技术要点
- 【C/C++语法】Chapter 1
- Java —内部类
- 排序算法nth_element()和partition()
- HDU4046 Panda(线段树)
- 1708 Fibonacci String
- 复杂网络中聚类算法总结
- ps学习笔记
- VOIP学习笔记
- Mysql优化(三)sql语句优化