From f26be91f7ba4abbc0ab2776d612bf026a29b4161 Mon Sep 17 00:00:00 2001 From: htabuchi Date: Mon, 7 Jul 2025 16:27:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?painza=E3=81=AE=E5=95=8F=E9=A1=8CB040?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/B040_htabuchi.java | 222 ++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 htabuchi/src/B040_htabuchi.java diff --git a/htabuchi/src/B040_htabuchi.java b/htabuchi/src/B040_htabuchi.java new file mode 100644 index 0000000..d649245 --- /dev/null +++ b/htabuchi/src/B040_htabuchi.java @@ -0,0 +1,222 @@ +/** + * 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; + +public class B040_htabuchi { + + /** + * 復号文の情報を格納する + */ + private 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; + } + + } + + /** + * 暗号文を解読するために必要な情報を格納する + */ + private 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; + } + + } + + /** + * 途中過程、もしくは結果の文字と文字リストを格納する + */ + public 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 static void main(String[] args) { + B040_htabuchi program = new B040_htabuchi(); + RequiredDetectiveData dto = program.scanningRequiredValue(); + final List resultSentence = program.decodeCipherSentence(dto); + 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 dto = new RequiredDetectiveData(countChange, betweenAlphaChangeMap, + cipherList); + + return dto; + } + + /** + * 変換文の単語がアルファベット文のどこに位置するかマップを作る + * + * @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 RequiredDetectiveData 解読用データ + */ + public List decodeCipherSentence(RequiredDetectiveData dto) { + List originSentence = new ArrayList<>(); // 解読後の文章保管 + + ProcessData nextCipher; // 置換用文字列 + + for (SentenceCipher cipher : dto.getCipherList()) { + nextCipher = new ProcessData(cipher.getMinCipher()); + + for (int count = 0; count < dto.getCountChange(); count++) { + List charaCipherList = findCharaList(dto.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; + } + + /** + * 結果表示 + * + * @param resultSentence 結果を入れた変数 + */ + public static void displayResultSentence(List resultSentence) { + System.out.println(String.join(" ", resultSentence)); + } + +} -- GitLab From d4b0d2eefe215ba38d1d312a5dfe1adbe8ab1788 Mon Sep 17 00:00:00 2001 From: htabuchi Date: Mon, 7 Jul 2025 17:52:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB040?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/B040_htabuchi.java | 144 +++++++++++++++----------------- 1 file changed, 68 insertions(+), 76 deletions(-) diff --git a/htabuchi/src/B040_htabuchi.java b/htabuchi/src/B040_htabuchi.java index d649245..315dcb8 100644 --- a/htabuchi/src/B040_htabuchi.java +++ b/htabuchi/src/B040_htabuchi.java @@ -12,92 +12,89 @@ import java.util.List; import java.util.Map; import java.util.Scanner; -public class B040_htabuchi { - - /** - * 復号文の情報を格納する - */ - private class SentenceCipher { - public SentenceCipher(String sentenceCipher, final List minCipher) { - this.sentenceCipher = sentenceCipher; - this.minCipher = minCipher; - } - - private String sentenceCipher; - private List minCipher; +/** + * 復号文の情報を格納する + */ +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; - } + /** 以下ゲッター */ + public List getMinCipher() { + return this.minCipher; } +} - /** - * 暗号文を解読するために必要な情報を格納する - */ - private 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; - } +/** + * 暗号文を解読するために必要な情報を格納する + */ +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 Map getCreateMapBetweenAlphaChange() { - return this.createMapBetweenAlphaChange; - } + /** 以下ゲッターとセッター */ + public int getCountChange() { + return this.countChange; + } - public List getCipherList() { - return this.cipherList; - } + public Map getCreateMapBetweenAlphaChange() { + return this.createMapBetweenAlphaChange; + } + public List getCipherList() { + return this.cipherList; } - /** - * 途中過程、もしくは結果の文字と文字リストを格納する - */ - public class ProcessData { - public ProcessData(final List resultCharaList) { - this.resultCharaList = resultCharaList; - } +} - private List resultCharaList; +/** + * 途中過程、もしくは結果の文字と文字リストを格納する + */ +class ProcessData { + public ProcessData(final List resultCharaList) { + this.resultCharaList = resultCharaList; + } - public List getResultCharaList() { - return this.resultCharaList; - } + private List resultCharaList; - public String joinChara() { - StringBuilder sb = new StringBuilder(); - for (char resultChara : resultCharaList) { - sb.append(resultChara); - } - final String resultSenc = sb.toString(); + public List getResultCharaList() { + return this.resultCharaList; + } - return resultSenc; + 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 dto = program.scanningRequiredValue(); - final List resultSentence = program.decodeCipherSentence(dto); + RequiredDetectiveData allData = program.scanningRequiredValue(); + final List resultSentence = program.decodeCipherSentence(allData); displayResultSentence(resultSentence); } @@ -127,10 +124,10 @@ public class B040_htabuchi { sc.close(); /** 利用するデータの格納先 */ - RequiredDetectiveData dto = new RequiredDetectiveData(countChange, betweenAlphaChangeMap, + RequiredDetectiveData allData = new RequiredDetectiveData(countChange, betweenAlphaChangeMap, cipherList); - return dto; + return allData; } /** @@ -156,7 +153,7 @@ public class B040_htabuchi { /** * 暗号用char配列 * - * @param sentenceDecording 1文の暗号文 + * @param wordCipher 1文の暗号文 */ public static List decomposeString(String wordCipher) { final List charaArray = new ArrayList<>(); @@ -170,18 +167,18 @@ public class B040_htabuchi { /** * 複号文を使って暗号文を解読する * - * @param RequiredDetectiveData 解読用データ + * @param allData 解読用データ */ - public List decodeCipherSentence(RequiredDetectiveData dto) { + public List decodeCipherSentence(RequiredDetectiveData allData) { List originSentence = new ArrayList<>(); // 解読後の文章保管 ProcessData nextCipher; // 置換用文字列 - for (SentenceCipher cipher : dto.getCipherList()) { + for (SentenceCipher cipher : allData.getCipherList()) { nextCipher = new ProcessData(cipher.getMinCipher()); - for (int count = 0; count < dto.getCountChange(); count++) { - List charaCipherList = findCharaList(dto.getCreateMapBetweenAlphaChange(), + for (int count = 0; count < allData.getCountChange(); count++) { + List charaCipherList = findCharaList(allData.getCreateMapBetweenAlphaChange(), nextCipher.getResultCharaList()); nextCipher = new ProcessData(charaCipherList); } @@ -210,11 +207,6 @@ public class B040_htabuchi { return findCharList; } - /** - * 結果表示 - * - * @param resultSentence 結果を入れた変数 - */ public static void displayResultSentence(List resultSentence) { System.out.println(String.join(" ", resultSentence)); } -- GitLab