Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SloganRecursion
- {
- class Program
- {
- static int n;
- static string[] words;
- static string slogan;
- static bool success;
- static StringBuilder resultSb = new StringBuilder();
- static void Main()
- {
- n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- words = Console.ReadLine().Split();
- slogan = Console.ReadLine();
- success = false;
- StringBuilder temp;
- for (int j = 0; j < words.Length; j++)
- {
- temp = new StringBuilder();
- Recursion(words[j], 0, temp);
- if (success)
- {
- break;
- }
- }
- if (!success)
- {
- resultSb.AppendLine("NOT VALID");
- //Console.WriteLine("NOT VALID");
- }
- }
- Console.Write(resultSb);
- }
- static void Recursion(string currentWord, int index, StringBuilder temp)
- //static void Recursion(string currentWord, int index, string currentResult)
- {
- if (index >= slogan.Length)
- {
- temp.Length--;
- success = true;
- //currentResult = currentResult.TrimStart();
- resultSb.AppendLine(temp.ToString());
- return;
- }
- if (slogan.IndexOf(currentWord, index) != index)
- {
- return;
- }
- temp.AppendFormat("{0} ", currentWord);
- //currentResult = string.Format("{0} {1}", currentResult, currentWord);
- int boundIndex = index + currentWord.Length;
- foreach (var word in words)
- {
- if (word.Length + index <= slogan.Length && !success)
- {
- Recursion(word, boundIndex, temp);
- //Recursion(word, boundIndex, currentResult);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement