Saturday, September 22, 2012

Problem Links:



  Crypt Kicker II 

A common but insecure method of encrypting text is to permute the letters of the alphabet. That is, in the text, each letter of the alphabet is consistently replaced by some other letter. So as to ensure that the encryption is reversible, no two letters are replaced by the same letter.
A common method of cryptanalysis is the known plaintext attack. In a known plaintext attack, the cryptanalist manages to have a known phrase or sentence encrypted by the enemy, and by observing the encrypted text then deduces the method of encoding.
Your task is to decrypt several encrypted lines of text, assuming that each line uses the same set of replacements, and that one of the lines of input is the encrypted form of the plaintext

the quick brown fox jumps over the lazy dog


The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input consists of several lines of input. Each line is encrypted as described above. The encrypted lines contain only lower case letters and spaces and do not exceed 80 characters in length. There are at most 100 input lines.


For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Source Code:


class Main {

    private static final String plain = "the quick brown fox jumps over the lazy dog";
    private int count;
    private String[] matrix;
    private int[] map;

    public static void main(String[] args) throws Exception {
 Main main = new Main();;

    public void run(String[] args) throws Exception {
 BufferedReader br = new BufferedReader(new InputStreamReader(;
 int cases = Integer.parseInt(br.readLine());
 String tmp = br.readLine();
 matrix = new String[100];
 while (cases-- > 0) {
     count = 0;
     boolean found = false;
     while ((tmp = br.readLine()) != null && tmp.length() != 0) {
  matrix[count] = tmp;
  found = found || checkLine(tmp);
     if (found) {
     } else {
  System.out.println("No solution.");
     if (cases > 0)

    public void print() {
 for (int i = 0; i < count; i++) {
     int len = matrix[i].length();
     for (int j = 0; j < len; j++) {
  if (matrix[i].charAt(j) == ' ')
      System.out.print(' ');
      System.out.print((char) map[matrix[i].charAt(j) - 'a']);

    public boolean checkLine(String aLine) {
 int len = aLine.length();
 if (plain.length() != len)
     return false;
 map = new int[26];
 for (int i = 0; i < len; i++) {
     int index1 = aLine.charAt(i) - 'a';
     if (aLine.charAt(i) == ' ') {
  if (plain.charAt(i) != ' ')
      return false;
     } else {
  if (map[index1] == 0)
      map[index1] = plain.charAt(i);
  else if (map[index1] != plain.charAt(i))
      return false;
 return true;