Advertisement
svetlai

MessageInABottle

Dec 1st, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Recursion
  8. {
  9.     class MessageInABottle
  10.     {
  11.         static List<KeyValuePair<char, string>> ciphers = new List<KeyValuePair<char, string>>();
  12.         static string message;
  13.        
  14.         public static void Main()
  15.         {
  16.             message = Console.ReadLine();
  17.             string cipher = Console.ReadLine();
  18.  
  19.             char key = char.MinValue;
  20.             StringBuilder value = new StringBuilder();
  21.  
  22.             for (int i = 0; i < cipher.Length; i++)
  23.             {
  24.                 //if (char.IsLetter(chipher[i]))  
  25.                 if (cipher[i] >= 'A' && cipher[i] <= 'Z')
  26.                 {
  27.                     if (key != char.MinValue)
  28.                     {
  29.                         var pair = new KeyValuePair<char, string>(key, value.ToString());
  30.                         ciphers.Add(pair);
  31.                         value.Clear();
  32.                     }
  33.                     key = cipher[i];
  34.                 }
  35.                 else
  36.                 {
  37.                     value.Append(cipher[i]);
  38.                 }
  39.             }
  40.  
  41.             if (key != char.MinValue)
  42.             {
  43.                 var pair = new KeyValuePair<char, string>(key, value.ToString());
  44.                 ciphers.Add(pair);
  45.                 value.Clear();
  46.             }
  47.  
  48.             Solve(0, new StringBuilder());
  49.            
  50.             Console.WriteLine(solutions.Count);
  51.             //solutions.Sort();
  52.             foreach (var solution in solutions)
  53.             {
  54.                 Console.WriteLine(solution);
  55.             }
  56.         }
  57.  
  58.         static SortedSet<string> solutions = new SortedSet<string>();
  59.        
  60.         static void Solve(int secretMsgIndex, StringBuilder sb)
  61.         {
  62.             if (secretMsgIndex == message.Length)
  63.             {
  64.                 solutions.Add(sb.ToString());
  65.                 return;
  66.             }
  67.  
  68.             foreach (var cipher in ciphers)
  69.             {
  70.                 if (message.Substring(secretMsgIndex).StartsWith(cipher.Value))
  71.                 {
  72.                     sb.Append(cipher.Key);
  73.                     Solve(secretMsgIndex + cipher.Value.Length, sb);
  74.                     //sb.Remove(sb.Length - 2, 1);
  75.                     sb.Length--;
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement