ellapt

DSA.MessagesInBottle

Jun 24th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace P1.MessagesInBottle
  7. {
  8.     class MessagesInBottle
  9.     {
  10.         static void Main()
  11.         {
  12.             string secretCode = Console.ReadLine();
  13.             string cipher = Console.ReadLine();
  14.  
  15.             CipherDecoder decoder = new CipherDecoder(secretCode, cipher);
  16.  
  17.             List<string> originalMessages = decoder.Decode();
  18.  
  19.             originalMessages.Sort();
  20.  
  21.             Console.WriteLine(originalMessages.Count);
  22.             foreach (string message in originalMessages)
  23.             {
  24.                 Console.WriteLine(message);
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Text;
  34.  
  35. namespace P1.MessagesInBottle
  36. {
  37.     class CipherElement
  38.     {
  39.         public char Letter { get; set; }
  40.         public string Code { get; set; }
  41.  
  42.         public CipherElement(char letter, string code)
  43.         {
  44.             this.Letter = letter;
  45.             this.Code = code;
  46.         }
  47.     }
  48. }
  49.  
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53. using System.Text;
  54.  
  55. namespace P1.MessagesInBottle
  56. {
  57.     /// <summary>
  58.     /// Decodes a secretCode message (using List<CipherElement> cipherElements key) into List<string> originalMessages
  59.     /// </summary>
  60.     class CipherDecoder
  61.     {
  62.         readonly List<CipherElement> cipherElements;
  63.         readonly string secretCode;
  64.         List<string> originalMessages;
  65.  
  66.         /// <summary>
  67.         /// Forms the cipherElements list of type <CipherElement> from the cipher
  68.         /// with the letter/ciphers tupples and takes the secret code which is to be decoded
  69.         /// </summary>
  70.         /// <param name="secretCode">The secret code (message1)</param>
  71.         /// <param name="cipher">The cipher (message2)</param>
  72.         public CipherDecoder(string secretCode, string cipher)
  73.         {
  74.             cipherElements = new List<CipherElement>();
  75.  
  76.             StringBuilder currentCipher = new StringBuilder();
  77.             char lastChar = '\0';
  78.             foreach (char ch in cipher)
  79.             {
  80.                 if (ch >= 'A' && ch <= 'Z') // is it a Latin capital letter?
  81.                 {
  82.                     if (currentCipher.Length > 0)
  83.                     {
  84.                         cipherElements.Add(new CipherElement(lastChar, currentCipher.ToString()));
  85.                         currentCipher.Clear();
  86.                     }
  87.                     lastChar = ch;
  88.                 }
  89.                 else
  90.                 {
  91.                     currentCipher.Append(ch);
  92.                 }
  93.             }
  94.             if (currentCipher.Length > 0)
  95.             {
  96.                 cipherElements.Add(new CipherElement(lastChar, currentCipher.ToString()));
  97.             }
  98.  
  99.             this.secretCode = secretCode;
  100.         }
  101.  
  102.         char[] currentOriginalMessage = new char[100];
  103.  
  104.         /// <summary>
  105.         /// Adds the current original message found to the result list
  106.         /// </summary>
  107.         /// <param name="currentOriginalMessage">Current version of the original message decoded</param>
  108.         private void AddSolution(char[] currentOriginalMessage)
  109.         {
  110.             StringBuilder originalMessage = new StringBuilder();
  111.             foreach (char c in currentOriginalMessage)
  112.             {
  113.                 if (c < 'A' || c > 'Z') break;
  114.                 originalMessage.Append(c);
  115.             }
  116.             originalMessages.Add(originalMessage.ToString());
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Decodes the message using recursion
  121.         /// </summary>
  122.         /// <param name="index">current index of the secret code</param>
  123.         /// <param name="wordIndex">current index in the solution list</param>
  124.         private void DecodeWithRecursion(int index, int wordIndex)
  125.         {
  126.             if (index == secretCode.Length)
  127.             {
  128.                 AddSolution(currentOriginalMessage);
  129.                 return;
  130.             }
  131.             if (index > secretCode.Length) return;
  132.             foreach (var element in cipherElements)
  133.             {
  134.                 if (secretCode.Length >= index + element.Code.Length
  135.                     && secretCode.Substring(index, element.Code.Length) == element.Code)
  136.                 {
  137.                     currentOriginalMessage[wordIndex] = element.Letter;
  138.                     DecodeWithRecursion(index + element.Code.Length, wordIndex + 1);
  139.                     currentOriginalMessage[wordIndex] = '\0';
  140.                 }
  141.             }
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Decodes the original messages using DecodeWithRecursion() method and returns the result
  146.         /// </summary>
  147.         /// <returns>result - the original message decoded</returns>
  148.         public List<string> Decode()
  149.         {
  150.             originalMessages = new List<string>();
  151.             DecodeWithRecursion(0, 0);
  152.             return originalMessages;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment