Advertisement
MeliDragon

Untitled

May 10th, 2023
32
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. var originalMessages = GetOriginalMessages(secretCode, cipherDict);
  39.  
  40. Console.WriteLine(originalMessages.Count);
  41. foreach (var message in originalMessages.OrderBy(m => m))
  42. {
  43. Console.WriteLine(message);
  44. }
  45. }
  46.  
  47. static List<string> GetOriginalMessages(string secretCode, Dictionary<string, char> cipherDict)
  48. {
  49. List<string> originalMessages = new List<string>();
  50. GenerateMessages(secretCode, cipherDict, "", originalMessages);
  51. return originalMessages;
  52. }
  53.  
  54. static void GenerateMessages(string secretCode, Dictionary<string, char> cipherDict, string currentMessage, List<string> originalMessages)
  55. {
  56. if (secretCode.Length == 0)
  57. {
  58. originalMessages.Add(currentMessage);
  59. return;
  60. }
  61.  
  62. foreach (var kvp in cipherDict)
  63. {
  64. var code = kvp.Key;
  65. var letter = kvp.Value;
  66.  
  67. if (secretCode.StartsWith(code))
  68. {
  69. var remainingCode = secretCode.Substring(code.Length);
  70. var newMessage = currentMessage + letter;
  71. GenerateMessages(remainingCode, cipherDict, newMessage, originalMessages);
  72. }
  73. }
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement