Advertisement
gosharski

Cipher

Mar 14th, 2021
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Cipher
  6. {
  7. class Program
  8. {
  9. static Dictionary<string, char> dic = new Dictionary<string, char>();
  10. static HashSet<string> variations = new HashSet<string>();
  11.  
  12. static void Main(string[] args)
  13. {
  14. string n = Console.ReadLine();
  15.  
  16. string cipher = Console.ReadLine();
  17.  
  18. FindCipher(cipher, 1, cipher[0], "");
  19.  
  20. Decipher(n, "", 0, "");
  21.  
  22. Console.WriteLine(variations.Count);
  23. List<string> list = variations.ToList();
  24. list.Sort();
  25. Console.WriteLine(string.Join(Environment.NewLine, list));
  26. }
  27.  
  28. static void Decipher(string n, string currentResult, int i, string currentKey)
  29. {
  30. if (i == n.Length)
  31. {
  32. return;
  33. }
  34.  
  35. currentKey += n[i];
  36.  
  37. if (dic.ContainsKey(currentKey))
  38. {
  39. currentResult += dic[currentKey];
  40. Decipher(n, currentResult, i + 1, "");
  41.  
  42. if (i == n.Length - 1)
  43. {
  44. variations.Add(currentResult);
  45. }
  46.  
  47. currentResult = currentResult.Remove(currentResult.Length - 1, 1);
  48. }
  49.  
  50. Decipher(n, currentResult, i + 1, currentKey);
  51. }
  52.  
  53. static void FindCipher(string cipher, int i, char c, string code)
  54. {
  55. if (i == cipher.Length)
  56. {
  57. dic.Add(code, c);
  58.  
  59. return;
  60. }
  61.  
  62. if (!Char.IsDigit(cipher[i]))
  63. {
  64. dic.Add(code, c);
  65. FindCipher(cipher, i + 1, cipher[i], "");
  66. }
  67. else
  68. {
  69. code += cipher[i];
  70. FindCipher(cipher, i + 1, c, code);
  71. }
  72. }
  73. }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement