Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Cipher
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- string secretCode = Console.ReadLine();
- string cipher = Console.ReadLine();
- var cipherDict = new Dictionary<string, char>();
- do
- {
- char letter = cipher[0];
- var builder = new StringBuilder();
- for (int i = 1; i <= cipher.Length; i++)
- {
- if (cipher.Length == i)
- {
- cipher = "";
- break;
- }
- if (!char.IsDigit(cipher[i]))
- {
- cipher = cipher.Substring(i);
- break;
- }
- builder.Append(cipher[i]);
- }
- cipherDict.Add(builder.ToString(), letter);
- } while (cipher.Length != 0);
- var originalMessages = GetOriginalMessages(secretCode, cipherDict);
- Console.WriteLine(originalMessages.Count);
- foreach (var message in originalMessages.OrderBy(m => m))
- {
- Console.WriteLine(message);
- }
- }
- static List<string> GetOriginalMessages(string secretCode, Dictionary<string, char> cipherDict)
- {
- List<string> originalMessages = new List<string>();
- GenerateMessages(secretCode, cipherDict, "", originalMessages);
- return originalMessages;
- }
- static void GenerateMessages(string secretCode, Dictionary<string, char> cipherDict, string currentMessage, List<string> originalMessages)
- {
- if (secretCode.Length == 0)
- {
- originalMessages.Add(currentMessage);
- return;
- }
- foreach (var kvp in cipherDict)
- {
- var code = kvp.Key;
- var letter = kvp.Value;
- if (secretCode.StartsWith(code))
- {
- var remainingCode = secretCode.Substring(code.Length);
- var newMessage = currentMessage + letter;
- GenerateMessages(remainingCode, cipherDict, newMessage, originalMessages);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement