AOJ 0017 Caesar Chipher
方針
単語を1文字ずつずらしていきます。「ただし、暗号化する前のデータは the, this, that という単語のいずれかを含む英語の文章ですので〜」とあるので、ずらしていく途中でそれらの単語を検出すると、ずらすのをやめます。
word[j] = (char)((word[j] - 'a' + 1)%26 +'a');
この辺、ややこしいですね。「シーザー暗号」等でぐぐるとページがたくさん出てくるのでそこからパク参考にしましょう。
あと、forループも汚くなってしまったのが心残りです。美しくない。一応、
k…ずらし方。最大で25回。
i…単語についてのループ。
j…単語の文字についてのループ。
となっています。
ソース
import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static String[] input; public static void main(String[] args) { while(read()){ solve(); } } static boolean read(){ if(!sc.hasNext())return false; input = sc.nextLine().split(" "); return true; } static void solve(){ boolean found = false; for(int k = 0; k < 26; k++){ for(int i = 0; i < input.length; i++){ char[] word = input[i].toCharArray(); for(int j = 0; j < word.length; j++){ if(Character.isLowerCase(word[j])){ word[j] = (char)((word[j] - 'a' + 1)%26 +'a'); } } input[i] = new String(word); if(input[i].equals("the") || input[i].equals("this") || input[i].equals("that"))found = true; } if(found)break; } for(int i = 0; i < input.length-1; i++){ System.out.print(input[i] + " "); } System.out.println(input[input.length-1]); } }