Advertisement
MeliDragon

Untitled

May 10th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Cipher
  7. {
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string secretCode = Console.ReadLine();
  13. string cipher = Console.ReadLine();
  14.  
  15. var cipherDict = new Dictionary<string, char>();
  16.  
  17. do
  18. {
  19. char letter = cipher[0];
  20. var builder = new StringBuilder();
  21. for (int i = 1; i <= cipher.Length; i++)
  22. {
  23. if (cipher.Length == i)
  24. {
  25. cipher = "";
  26. break;
  27. }
  28. if (!char.IsDigit(cipher[i]))
  29. {
  30. cipher = cipher.Substring(i);
  31. break;
  32. }
  33. builder.Append(cipher[i]);
  34. }
  35. cipherDict.Add(builder.ToString(), letter);
  36.  
  37. } while (cipher.Length != 0);
  38.  
  39. var originalMessages = GetOriginalMessages(secretCode, cipherDict);
  40.  
  41. Console.WriteLine(originalMessages.Count);
  42. foreach (var message in originalMessages.OrderBy(m => m))
  43. {
  44. Console.WriteLine(message);
  45. }
  46. }
  47.  
  48. static List<string> GetOriginalMessages(string secretCode, Dictionary<string, char> cipherDict)
  49. {
  50. List<string> originalMessages = new List<string>();
  51. GenerateMessages(secretCode, cipherDict, "", originalMessages);
  52. return originalMessages;
  53. }
  54.  
  55. static void GenerateMessages(string secretCode, Dictionary<string, char> cipherDict, string currentMessage, List<string> originalMessages)
  56. {
  57. if (secretCode.Length == 0)
  58. {
  59. originalMessages.Add(currentMessage);
  60. return;
  61. }
  62.  
  63. foreach (var kvp in cipherDict)
  64. {
  65. var code = kvp.Key;
  66. var letter = kvp.Value;
  67.  
  68. if (secretCode.StartsWith(code))
  69. {
  70. var remainingCode = secretCode.Substring(code.Length);
  71. var newMessage = currentMessage + letter;
  72. GenerateMessages(remainingCode, cipherDict, newMessage, originalMessages);
  73. }
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement