nassss

Untitled

Mar 15th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _14._Slogan
  6. {
  7. class Program
  8. {
  9. static HashSet<string> collection = new HashSet<string>();
  10. static void Main(string[] args)
  11. {
  12. var n = int.Parse(Console.ReadLine());
  13. if (n<1 || n > 10)
  14. {
  15. throw new ArgumentOutOfRangeException("N must be a valid integer between the following range 1 <= N <= 10.");
  16. }
  17.  
  18. for (int i = 0; i < n; i++)
  19. {
  20. var words = Console.ReadLine().Split(' ').ToArray();
  21. var slogan = Console.ReadLine();
  22.  
  23. if (!isValid(words, slogan, string.Empty))
  24. {
  25. Console.WriteLine("NOT VALID");
  26. }
  27. }
  28. }
  29. static bool isValid (string[] words, string slogan, string output )
  30. {
  31. if (slogan.Length <= 0)
  32. {
  33. Console.WriteLine(output);
  34. return true;
  35. }
  36. if (!collection.Add(slogan))
  37. {
  38. return false;
  39. }
  40. foreach (var item in words)
  41. {
  42. if (slogan.StartsWith(item))
  43. {
  44. if (isValid(words,slogan.Substring(item.Length), output + item + " "))
  45. {
  46. return true;
  47. }
  48. else
  49. {
  50. collection.Add(slogan);
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment