Advertisement
stanevplamen

02.09.04.02.MessageInBottle

Jul 12th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class MessageInBottle
  6. {
  7.     static void Main()
  8.     {
  9.         string inputNumbersString = "1122";
  10.         string textToDecode = "A1B12C11D2";
  11.         mainArrChars = new List<char>();
  12.         mainArrNumbs = new List<string>();
  13.         StringBuilder sb = new StringBuilder();
  14.  
  15.         for (int i = 0; i < textToDecode.Length; i++)
  16.         {
  17.             int currentCharValue = (int)textToDecode[i];
  18.             if (currentCharValue >= 65 && currentCharValue <= 90)
  19.             {
  20.                 mainArrNumbs.Add(sb.ToString());
  21.                 mainArrChars.Add((char)currentCharValue);
  22.                 sb = new StringBuilder();
  23.             }
  24.             else
  25.             {
  26.                 sb.Append((char)currentCharValue);
  27.             }
  28.         }
  29.         mainArrNumbs.Add(sb.ToString());
  30.         CalculateCodes(inputNumbersString, "");
  31.         Print();
  32.     }
  33.     static List<string> mainArrNumbs = new List<string>();
  34.     static List<char> mainArrChars;
  35.     static List<string> finalOutput = new List<string>();
  36.  
  37.     static void Print(string[] vector)
  38.     {
  39.         for (int i = 0; i < vector.Length; i++)
  40.         {
  41.             Console.Write("{0} ",vector[i]);
  42.         }
  43.         Console.WriteLine();
  44.     }
  45.  
  46.     static void CalculateCodes(string currentCode, string codeInQueue)
  47.     {
  48.         if (currentCode.Length == 0)
  49.         {
  50.             finalOutput.Add(codeInQueue);
  51.             return;
  52.         }
  53.         for (int i = 1; i < mainArrNumbs.Count ; i++)
  54.         {
  55.             if (currentCode.StartsWith(mainArrNumbs[i]))
  56.             {
  57.                 CalculateCodes(currentCode.Substring(mainArrNumbs[i].Length) , codeInQueue + mainArrChars[i - 1]);
  58.             }
  59.         }
  60.     }
  61.  
  62.     static void Print()
  63.     {
  64.         Console.WriteLine(finalOutput.Count);
  65.         finalOutput.Sort();
  66.         foreach (string comb in finalOutput)
  67.         {
  68.             Console.WriteLine(comb);
  69.         }                    
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement