You know Galois is a very smart student. He dislikes History exams and you know that he will find a smart way to cheat. You are the supervisor in the exam hall for his History exam and you recover from him a sheet of paper that contains several lines that appear to be in a language not even remotely resembling English even though the alphabets used are from the English language. The Principal will not believe you unless you establish beyond doubt, that the paper recovered from him contains an answer to one of the questions. You show the paper to the head of Computer Science department and he says that it is a well-known crypto method and agrees to decrypt the same for you. It turns out that Galois did the following,
He writes the original message in a zig zag pattern and reads of the lines horizontally. For example, IAMSMARTERTHANYOU is written first as and encoded as IEUATROMRTYSAHNMA
Given the number of rows used by Galois to encode the text and the encoded text, write a program to recover the original text. Note that if the length of the string is not adequate to complete the pattern, Galois pads it with the character “X” to make up the length. These must not appear in the output.
Input Format:
The first line of the input is the number of rows (depth) Galois used in his coding scheme
The next line is the encoded message
Output Format:
The decoded message with all the padding characters (if any) removed.
Constraints:
Depth ≤10
Example 1
Input
5
CCEAEOSNDDEYUEHOT
Output
CANYOUDECODETHESE
Explanation
The depth is 5. When the input message is laid out, it looks like this
Hence the output is CANYOUDECODETHESE
Example 2
Input
5
WTXHUTXAOHXTBIXAS
Output
WHATABOUTTHIS
Explanation
The depth is 5. When the message is laid out, it appears as
The apparent decoded message is WHATABOUTTHISXXXX. But the X’s are padding characters. Hence the output is WHATABOUTTHIS.
Hey… I wanted to code this in java but here is the C++ version of the decrypter.
One can also create another module in this program to cipher codes in Railfence Cipher algorithm.
#include <bits/stdc++.h>
using namespace std;
string decryptRailFence(string cipher, int key)
{
char rail[key][cipher.length()];
for (int i=0; i < key; i++)
for (int j=0; j < cipher.length(); j++)
rail[i][j] = ‘\n’;
bool dir_down;
int row = 0, col = 0;
for (int i=0; i < cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
rail[row][col++] = ‘’;
dir_down?row++ : row–;
}
int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '’ && index<cipher.length())
rail[i][j] = cipher[index++];
string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
if (rail[row][col] != ‘*’)
result.push_back(rail[row][col++]);
dir_down?row++: row–;
}
return result;
}
int main()
{
cout << decryptRailFence(“CCEAEOSNDDEYUEHOT”, 5) << endl;
cout << decryptRailFence(“WTXHUTXAOHXTBIXAS”, 5) << endl;
return 0;
}