Advertisement
Guest User

Slogan

a guest
Jan 21st, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 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 Slogan
  8. {
  9. class Program
  10. {
  11. static int n;
  12. static string[] words;
  13. static string slogan;
  14. static StringBuilder resultSb = new StringBuilder();
  15. static StringBuilder tempSb = new StringBuilder();
  16. static void Main()
  17. {
  18. n = int.Parse(Console.ReadLine());
  19. for (int i = 0; i < n; i++)
  20. {
  21. words = Console.ReadLine().Split();
  22. slogan = Console.ReadLine();
  23. string currentResult = slogan;
  24. foreach (string word in words)
  25. {
  26. int timesMet = 0;
  27. if (currentResult.StartsWith(word))
  28. {
  29. timesMet++;
  30. }
  31. if (currentResult.EndsWith(word) && currentResult.Length > word.Length)
  32. {
  33. timesMet++;
  34. }
  35. string[] splitResult = currentResult.Split(new string[] { word }, StringSplitOptions.RemoveEmptyEntries);
  36. if (splitResult.Length > 1)
  37. {
  38. timesMet += splitResult.Length - 1;
  39. }
  40. currentResult = string.Join("", splitResult);
  41. if (timesMet > 0)
  42. {
  43. tempSb.Append(String.Join(" ", Enumerable.Repeat(word, timesMet)));
  44. tempSb.Append(' ');
  45. }
  46. }
  47. if (currentResult.Length > 0)
  48. {
  49. resultSb.AppendLine("NOT VALID");
  50. }
  51. else
  52. {
  53. while (slogan != string.Empty)
  54. {
  55. foreach (var word in words)
  56. {
  57. if (slogan.StartsWith(word))
  58. {
  59. resultSb.Append($"{word} ");
  60. slogan = slogan.Substring(word.Length);
  61.  
  62. if (slogan == string.Empty)
  63. {
  64. resultSb.AppendLine();
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. tempSb.Clear();
  72. }
  73. Console.WriteLine(resultSb.ToString().TrimEnd());
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement