Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 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 SloganRecursion
  8. {
  9.     class Program
  10.     {
  11.         static int n;
  12.         static string[] words;
  13.         static string slogan;
  14.         static bool success;
  15.         static StringBuilder resultSb = new StringBuilder();
  16.  
  17.         static void Main()
  18.         {
  19.             n = int.Parse(Console.ReadLine());
  20.             for (int i = 0; i < n; i++)
  21.             {
  22.                 words = Console.ReadLine().Split();
  23.                 slogan = Console.ReadLine();
  24.                 success = false;
  25.                 StringBuilder temp;
  26.                 for (int j = 0; j < words.Length; j++)
  27.                 {
  28.                     temp = new StringBuilder();
  29.                     Recursion(words[j], 0, temp);
  30.                     if (success)
  31.                     {
  32.                         break;
  33.                     }
  34.                 }
  35.                 if (!success)
  36.                 {
  37.                     resultSb.AppendLine("NOT VALID");
  38.                     //Console.WriteLine("NOT VALID");
  39.                 }
  40.             }
  41.             Console.Write(resultSb);
  42.         }
  43.  
  44.         static void Recursion(string currentWord, int index, StringBuilder temp)
  45.         //static void Recursion(string currentWord, int index, string currentResult)
  46.         {
  47.             if (index >= slogan.Length)
  48.             {
  49.                 temp.Length--;
  50.                 success = true;
  51.                 //currentResult = currentResult.TrimStart();
  52.                 resultSb.AppendLine(temp.ToString());
  53.                 return;
  54.             }
  55.  
  56.             if (slogan.IndexOf(currentWord, index) != index)
  57.             {
  58.                 return;
  59.             }
  60.  
  61.             temp.AppendFormat("{0} ", currentWord);
  62.             //currentResult = string.Format("{0} {1}", currentResult, currentWord);
  63.             int boundIndex = index + currentWord.Length;
  64.             foreach (var word in words)
  65.             {
  66.                 if (word.Length + index <= slogan.Length && !success)
  67.                 {
  68.                     Recursion(word, boundIndex, temp);
  69.                     //Recursion(word, boundIndex, currentResult);
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement