Advertisement
unrealbg

Untitled

May 11th, 2023
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. namespace Cipher
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             string secretMessage = Console.ReadLine();
  11.             string cipher = Console.ReadLine();
  12.             Dictionary<string, string> cipherDict = new Dictionary<string, string>();
  13.             List<List<string>> dp = new List<List<string>>();
  14.  
  15.             for (int i = 0; i < cipher.Length; i++)
  16.             {
  17.                 char letter = cipher[i];
  18.                 i++;
  19.                 string code = "";
  20.                 while (i < cipher.Length && char.IsDigit(cipher[i]))
  21.                 {
  22.                     code += cipher[i];
  23.                     i++;
  24.                 }
  25.                 cipherDict[code] = letter.ToString();
  26.                 i--;
  27.             }
  28.  
  29.             for (int i = 0; i <= secretMessage.Length; i++)
  30.             {
  31.                 dp.Add(new List<string>());
  32.             }
  33.  
  34.             dp[0].Add("");
  35.  
  36.             for (int i = 0; i < secretMessage.Length; i++)
  37.             {
  38.                 for (int j = i + 1; j <= secretMessage.Length; j++)
  39.                 {
  40.                     string subStr = secretMessage.Substring(i, j - i);
  41.                     if (cipherDict.ContainsKey(subStr))
  42.                     {
  43.                         foreach (var str in dp[i])
  44.                         {
  45.                             dp[j].Add(str + cipherDict[subStr]);
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             Console.WriteLine(dp[secretMessage.Length].Count);
  52.             dp[secretMessage.Length].Sort();
  53.             foreach (string result in dp[secretMessage.Length])
  54.             {
  55.                 Console.WriteLine(result);
  56.             }
  57.         }
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement