Advertisement
sylviapsh

Message In A Bottle With Queue

Jun 23rd, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. namespace MessageInABottle
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     /// <summary>
  7.     /// In a warm, sunny day Andrew found a bottle near the sea. It was a very special bottle, containing not one, but two messages. The first message contained a big sequence of digits (0-9). “This must be a secret code”, Andrew thought. And he was right. After seeing the second message everything became clearer. The second message said something like this: “A123C11B98”. Another idea struck Andrew: “Hmm may be this is the cipher, used for creating the secret code”. And again he was right.
  8.     /// An alphabetical message, containing only capital English letters, is encoded with the given cipher. For every letter in the original message it is replaced by the given sequence of digits in the cipher.
  9.     /// For example an original message ABC with a cipher A123C11B98 will be encoded as 1239811.
  10.     /// Write a program that for a given secret code from the first bottle message and a given cipher from the second bottle message finds all possible original messages that can be encoded to the given secret code.
  11.     /// </summary>
  12.     public class MessageInABottle
  13.     {
  14.         public static void Main()
  15.         {
  16.             string message = Console.ReadLine();
  17.             string code = Console.ReadLine();
  18.  
  19.             Dictionary<string, char> cipher = new Dictionary<string, char>();
  20.             char letter = '.';
  21.             string digit = string.Empty;
  22.             for (int i = 0; i < code.Length; i++)
  23.             {
  24.                 if (char.IsLetter(code[i]))
  25.                 {
  26.                     if (digit != string.Empty)
  27.                     {
  28.                         cipher.Add(digit, letter);
  29.                         digit = string.Empty;
  30.                     }
  31.  
  32.                     letter = code[i];
  33.                 }
  34.                 else
  35.                 {
  36.                     digit += code[i];
  37.                 }
  38.             }
  39.  
  40.             if (digit != string.Empty)
  41.             {
  42.                 cipher.Add(digit, letter);
  43.             }
  44.  
  45.             List<string> results = new List<string>();
  46.  
  47.             Decode current = new Decode();
  48.             current.Solved = string.Empty;
  49.             current.Left = message;
  50.             Queue<Decode> possibleSolutions = new Queue<Decode>();
  51.             possibleSolutions.Enqueue(current);
  52.  
  53.             while (possibleSolutions.Count != 0)
  54.             {
  55.                 current = possibleSolutions.Dequeue();
  56.                 foreach (var item in cipher)
  57.                 {
  58.                     if (current.Left.StartsWith(item.Key))
  59.                     {
  60.                         Decode temp = new Decode();
  61.                         temp.Solved = current.Solved + item.Value;
  62.                         temp.Left = current.Left.Remove(0, item.Key.Length);
  63.  
  64.                         if (temp.Left.Length == 0)
  65.                         {
  66.                             results.Add(temp.Solved);
  67.                         }
  68.                         else
  69.                         {
  70.                             possibleSolutions.Enqueue(temp);
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             results.Sort();
  77.  
  78.             Console.WriteLine(results.Count);
  79.             foreach (var item in results)
  80.             {
  81.                 Console.WriteLine(item);
  82.             }
  83.         }
  84.  
  85.         public class Decode
  86.         {
  87.             public string Solved { get; set; }
  88.  
  89.             public string Left { get; set; }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement