首页 > 专题系列 > Java解POJ > POJ 1298 The Hardest Problem Ever [解题报告] Java
2013
11-09

POJ 1298 The Hardest Problem Ever [解题报告] Java

The Hardest Problem Ever

问题描述 :

Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.

You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text

V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

输入:

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

  1. Start line – A single line, “START”
  2. Cipher message – A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar
  3. End line – A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

输出:

For each data set, there will be exactly one line of output. This is the original message by Caesar.

样例输入:

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

样例输出:

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

解题代码:

import java.io.BufferedInputStream;
 import java.util.Scanner;

 public class Main {

     String cipher;
     char[] message;
     char c;

     public Main() {
         Scanner scan = new Scanner(new BufferedInputStream(System.in));
         while (!(cipher = scan.nextLine()).equals("ENDOFINPUT")) {
             while (!(cipher = scan.nextLine()).equals("END")) {
                 message = cipher.toCharArray();
                 for (int i = 0; i < message.length; i++) {
                     c = message[i];
                     if (65 <= c && c <= 90) {
                         c = (char) (65 + (c - 65 - 5 + 26) % 26);
                         message[i] = c;
                     }
                 }
                 System.out.println(String.valueOf(message));
             }
         }
     }

     public static void main(String[] args) {
         new Main();
     } 
}

  1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  3. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。