sunfresh

Cipher

Jan 24th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. Cipher
  2. In a warm, sunny day Andrew found a bottle near the sea. It was a very special bottle, containing not one, but two messages. The first message contained a big sequence of digits (0-9). “This must be a secret code”, Andrew thought. And he was right. After seeing the second message everything became clearer. The second message said something like this: “A123C11B98”. Another idea struck Andrew: “Hmm may be this is the cipher, used for creating the secret code”. And again he was right.
  3.  
  4. An alphabetical message, containing only capital English letters, is encoded with the given cipher. For every letter in the original message it is replaced by the given sequence of digits in the cipher.
  5.  
  6. For example an original message ABC with a cipher A123C11B98 will be encoded as 1239811.
  7.  
  8. Write a program that for a given secret code from the first bottle message and a given cipher from the second bottle message finds all possible original messages that can be encoded to the given secret code.
  9.  
  10. Input
  11. Read from the standard input
  12.  
  13. On the first input line there will be the secret message (the sequence of digits)
  14.  
  15. On the second input line there will be the cipher used for encoding the message in the given format: “{LetterX}{CodeForX}{LetterY}{CodeForY}…” where every LetterX from the original message will be encoded with CodeForX in the secret code
  16.  
  17. One letter will have only one unique representation.
  18. The input data will always be valid and in the format described. There is no need to check it explicitly.
  19.  
  20. Output
  21. Print to the standard output
  22.  
  23. In the first console line you must print the number of all possible original messages that can be encoded to the given secret code
  24.  
  25. On the next lines, print these messages, sorted alphabetically
  26.  
  27. See the examples bellow.
  28.  
  29. Constraints
  30. The given secret code will contain no more than 20 digits.
  31.  
  32. The cipher will be no longer than 180 symbols, containing only capital English letters and digits.
  33.  
  34. The number of original messages (the answers) will be no more than 2048.
  35.  
  36. import java.util.*;
  37. public class Cipher {
  38. public static void main(String[] args) {
  39. Scanner scanner = new Scanner(System.in);
  40. String secretCode = scanner.nextLine();
  41. String cipher = scanner.nextLine();
  42. HashMap<String, String> cipherMap = new HashMap<>();
  43. List<String> decodedMessages = new ArrayList<>();
  44. for (int i = 0; i < cipher.length(); i++) {
  45. if (Character.isUpperCase(cipher.charAt(i))) {
  46. String associatedNumber = "";
  47. for (int j = i + 1; j < cipher.length(); j++) {
  48. if (Character.isDigit(cipher.charAt(j))) {
  49. associatedNumber += Character.toString(cipher.charAt(j));
  50. } else break;
  51. }
  52. cipherMap.putIfAbsent(associatedNumber, Character.toString(cipher.charAt(i)));
  53. }
  54. }
  55. possibleMessages(secretCode, 0, "", cipherMap, decodedMessages);
  56. Collections.sort(decodedMessages);
  57. System.out.println(decodedMessages.size());
  58. for (String message : decodedMessages) {
  59. System.out.println(message);
  60. }
  61. }
  62. private static void possibleMessages(String secretCode,
  63. int index,
  64. String decodedMessage,
  65. HashMap<String, String> cipherMap,
  66. List<String> decodedMessages) {
  67. if (index == secretCode.length()) {
  68. decodedMessages.add(decodedMessage);
  69. return;
  70. }
  71. String currentMessage = "";
  72. for (int i = index; i < secretCode.length(); i++) {
  73. currentMessage += secretCode.charAt(i);
  74. if (cipherMap.containsKey(currentMessage)) {
  75. possibleMessages(secretCode, i + 1,
  76. decodedMessage + cipherMap.get(currentMessage),
  77. cipherMap, decodedMessages);
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment