# Problem: Permutation Cipher

Document Sample

Problem: Permutation Cipher
Filename: cipher.java
Input File: cipher.in
Difficulty: Easy

Problem Description
In today’s internet era, messages are usually encrypted to prevent eavesdropping. Many
sophisticated encryption techniques have been developed over the years, but this problem
deals with one of the simplest techniques known as permutation cipher. This approach
applies some permutation to t he letters of the message. For example, applying the
permutation <4, 2, 5, 1, 3> to the message “abcde” one gets the message “dbeac”. Your
task is to apply this encryption technique to a message.

The Input
There will be multiple test cases. The first input line contains a positive integer n,
indicating the number of data sets to be processed. The data sets will be on the following
input lines, each set consisting of two lines. The first of the two input lines for a data set
contains the message to be encrypted, starting in column 1, all lower-case letters, at least
one letter and at most 20 letters. The second input line provides the permutation to be
applied to the message. If the message has c characters, the second input lines will
contain integer values 1 through c, each value appearing exactly once (values are
separated from each other by one or more spaces).

The Output
At the beginning of each test case, output “Message #m:”, where m is the test case
number (starting from 1). Then, print the original message followed by the encrypted
message. Leave a blank line after the output for each test case. Follow the format
illustrated in Sample Output.

Sample Input
2
abcde
42513
cfu
312

Sample Output
Message #1:
Original: abcde
Encrypted: dbeac

Message #2:
Original: cfu
Encrypted: ucf
Further Analysis
The problem deals with a String followed by a list of positions for the characters in the
array. We’ll take the first sample case. The original string is “abcde”. The order for the
ciphered string is “4 2 5 1 3”. The easiest way to conceptualize solving this problem is
simply making an integer array for the ordering of the ciphered string, and just grabbing
the indexes of the original string until we are out of integers from the order array.

After reading in the input string and order array, we should be setting a for loop to
terminate at the end of Input String’s length. Here is an example of the running of the
loop

And now our for loop exits because it has hit the end of the length. So we now have our
original string intact as well as our new ciphered string. Now all we have to do is output
it.
The Code
import java.util.*;
import java.io.*;

public class cipher
{
public static void main(String[] args)
throws java.io.FileNotFoundException
{
Scanner fin = new Scanner(new File("cipher.in"));
String Original;
String Ciphered;
int Length;
int cases = fin.nextInt();
for(int y=0;y<cases;y++)
{
Original = "";
Ciphered = "";
System.out.println("Message #" +(y+1) +":");
Original = fin.next();
Length = Original.length();
for (int z=0;z<Length;z++)
{
Ciphered+=(Original.charAt(fin.nextInt()-1));
}
System.out.println("    Original: " + Original);
System.out.println("    Encrypted: " + Ciphered);
System.out.println();
}
}
}

Code Analysis
As you can see in the code, there is not an intermediary integer array of Order as
explained in the problem analysis. It is helpful to include it in order to conceptualize the
problem, but in the actual implementation it is shorter and more efficient to do without
the order, and just do the “permuting” as you read in each integer from the cipher. The int
Length is extraneous, but it speeds up the program slightly by not having to call the
procedure inside String.length() every time the for loop executes.

DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 3 posted: 9/11/2012 language: Unknown pages: 3
How are you planning on using Docstoc?