Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _1.MessageInABottle
  9. {
  10.     struct Particle
  11.     {
  12.         public Particle(char letter, string code) : this()
  13.         {
  14.             Letter = letter;
  15.             Code = code;
  16.         }
  17.  
  18.         public char Letter { get; set; }
  19.         public string Code { get; set; }
  20.     }
  21.  
  22.     class Program
  23.     {
  24.         static List<Particle> particles = new List<Particle>();
  25.         static List<string> results = new List<string>();
  26.         private static char letter;
  27.         private static string secretCode;
  28.         static void Main()
  29.         {
  30.             secretCode = Console.ReadLine();
  31.             string cipher = Console.ReadLine();
  32.             StringBuilder code = new StringBuilder();
  33.             for (int i = 0; i < cipher.Length; i++)
  34.             {
  35.                 if (cipher[i] >= 'A' && cipher[i] <= 'Z')
  36.                 {
  37.                     if (i != 0)
  38.                     {
  39.                         particles.Add(new Particle(letter, code.ToString()));
  40.                         code.Clear();
  41.                     }
  42.                     letter = cipher[i];
  43.                 }
  44.                 else
  45.                 {
  46.                     code.Append(cipher[i]);
  47.                 }
  48.             }
  49.             particles.Add(new Particle(letter, code.ToString()));
  50.  
  51.             Recursion(0);
  52.  
  53.             Console.WriteLine(results.Count);
  54.             results.Sort();
  55.             foreach (string result in results)
  56.             {
  57.                 Console.WriteLine(result);
  58.             }
  59.         }
  60.  
  61.         static StringBuilder currentResult = new StringBuilder();
  62.         private static void Recursion(int secretCodeIndex)
  63.         {
  64.  
  65.             if (secretCodeIndex >= secretCode.Length)
  66.             {
  67.                 results.Add(currentResult.ToString());
  68.                 return;
  69.             }
  70.  
  71.             foreach (Particle particle in particles)
  72.             {
  73.                 if (secretCode.Substring(secretCodeIndex).StartsWith(particle.Code))
  74.                 {
  75.                     currentResult.Append(particle.Letter);
  76.                     Recursion(secretCodeIndex + particle.Code.Length);
  77.                     currentResult.Remove(currentResult.Length - 1, 1);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement