diff --git a/htabuchi/src/B040_htabuchi.java b/htabuchi/src/B040_htabuchi.java new file mode 100644 index 0000000000000000000000000000000000000000..315dcb849da00789b3d00eaded26582729bd2171 --- /dev/null +++ b/htabuchi/src/B040_htabuchi.java @@ -0,0 +1,214 @@ +/** + * Paiza問題B040:楽しい暗号解読
+ * create 2025/07/04,07 + * + * @author 田渕日奈子 + * @version 1.0 + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +/** + * 復号文の情報を格納する + */ +class SentenceCipher { + public SentenceCipher(String sentenceCipher, final List minCipher) { + this.sentenceCipher = sentenceCipher; + this.minCipher = minCipher; + } + + private String sentenceCipher; + private List minCipher; + + /** 以下ゲッター */ + + public List getMinCipher() { + return this.minCipher; + } +} + +/** + * 暗号文を解読するために必要な情報を格納する + */ +class RequiredDetectiveData { + private int countChange; + private Map createMapBetweenAlphaChange; + private List cipherList; + + public RequiredDetectiveData(final int countChange, + final Map createMapBetweenAlphaChange, + final List cipherList) { + this.countChange = countChange; + this.createMapBetweenAlphaChange = createMapBetweenAlphaChange; + this.cipherList = cipherList; + } + + /** 以下ゲッターとセッター */ + public int getCountChange() { + return this.countChange; + } + + public Map getCreateMapBetweenAlphaChange() { + return this.createMapBetweenAlphaChange; + } + + public List getCipherList() { + return this.cipherList; + } + +} + +/** + * 途中過程、もしくは結果の文字と文字リストを格納する + */ +class ProcessData { + public ProcessData(final List resultCharaList) { + this.resultCharaList = resultCharaList; + } + + private List resultCharaList; + + public List getResultCharaList() { + return this.resultCharaList; + } + + public String joinChara() { + StringBuilder sb = new StringBuilder(); + for (char resultChara : resultCharaList) { + sb.append(resultChara); + } + final String resultSenc = sb.toString(); + + return resultSenc; + } +} + +public class B040_htabuchi { + /** + * メイン関数 + */ + public static void main(String[] args) { + B040_htabuchi program = new B040_htabuchi(); + RequiredDetectiveData allData = program.scanningRequiredValue(); + final List resultSentence = program.decodeCipherSentence(allData); + displayResultSentence(resultSentence); + } + + /** + * スキャンから置換回数、暗号文、復号文を読み取る + */ + public RequiredDetectiveData scanningRequiredValue() { + Scanner sc = new Scanner(System.in); + final int countChange = sc.nextInt(); // 置換回数 + final String sentenceChange = sc.next(); // 変換用文章 + + final Map betweenAlphaChangeMap = createMapBetweenAlphaChange( + sentenceChange); + sc.nextLine(); // 改行を飛ばす + + List cipherList = new ArrayList<>(); // 暗号文のリスト + + /** 複号文の数は未定 */ + final String sentenceCipher = sc.nextLine(); + final String[] wordsCipherArray = sentenceCipher.split(" "); + + for (String wordCipher : wordsCipherArray) { + List minCipher = decomposeString(wordCipher); + cipherList.add(new SentenceCipher(wordCipher, minCipher)); + } + + sc.close(); + + /** 利用するデータの格納先 */ + RequiredDetectiveData allData = new RequiredDetectiveData(countChange, betweenAlphaChangeMap, + cipherList); + + return allData; + } + + /** + * 変換文の単語がアルファベット文のどこに位置するかマップを作る + * + * @param sentenceChange 変換文 + */ + public Map createMapBetweenAlphaChange(String sentenceChange) { + + /** アルファベット文 */ + final String alphabetSentence = "abcdefghijklmnopqrstuvwxyz"; + + /** 変換文→アルファベット文の変換表map */ + final Map betweenAlphaChangeMap = new HashMap<>(); + + for (int numChara=0 ; numChara decomposeString(String wordCipher) { + final List charaArray = new ArrayList<>(); + for (char chara : wordCipher.toCharArray()) { + charaArray.add(chara); + } + + return charaArray; + } + + /** + * 複号文を使って暗号文を解読する + * + * @param allData 解読用データ + */ + public List decodeCipherSentence(RequiredDetectiveData allData) { + List originSentence = new ArrayList<>(); // 解読後の文章保管 + + ProcessData nextCipher; // 置換用文字列 + + for (SentenceCipher cipher : allData.getCipherList()) { + nextCipher = new ProcessData(cipher.getMinCipher()); + + for (int count = 0; count < allData.getCountChange(); count++) { + List charaCipherList = findCharaList(allData.getCreateMapBetweenAlphaChange(), + nextCipher.getResultCharaList()); + nextCipher = new ProcessData(charaCipherList); + } + + originSentence.add(nextCipher.joinChara()); + } + + return originSentence; + } + + /** + * 複号文の何番目に暗号文字があるか探索 + * + * @param createMapBetweenAlphaChange 変換文→アルファベット文の変換表map + * @param cipher 暗号文 + */ + public List findCharaList(Map createMapBetweenAlphaChange, + List cipher) { + List findCharList = new ArrayList<>(); + + for (char oneCipher : cipher) { + char nextCipher = createMapBetweenAlphaChange.get(oneCipher); + findCharList.add(nextCipher); + } + + return findCharList; + } + + public static void displayResultSentence(List resultSentence) { + System.out.println(String.join(" ", resultSentence)); + } + +}