Advertisement
plamen27

Melrah Shake - 3 solutions

Dec 28th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 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. class Program
  8. {
  9. static void Main(string[] args)
  10. { // my solution, below other solutions commented:
  11. string input = Console.ReadLine();
  12. string pattern = Console.ReadLine();
  13. while (true)
  14. {
  15.  
  16. //1Sample2ExamJun2016 3FootballStandings
  17. StringBuilder sb = new StringBuilder(input);
  18. int indexFirst = input.IndexOf(pattern);
  19. int indexLast = input.LastIndexOf(pattern);
  20. if (indexFirst > -1 && indexLast > -1 && pattern != String.Empty)
  21. {
  22. sb.Remove(indexFirst, pattern.Length);
  23. input = sb.ToString();
  24.  
  25. sb.Remove(input.LastIndexOf(pattern), pattern.Length); //indexLast-pattern.Length
  26. input = sb.ToString();
  27. //Console.WriteLine(input);
  28. Console.WriteLine("Shaked it.");
  29. sb = new StringBuilder(pattern);
  30. if (pattern.Length > 0)
  31. {
  32. int indexToRemove = pattern.Length / 2;
  33. //Console.WriteLine(indexToRemove);
  34. sb.Remove(indexToRemove, 1);
  35. pattern = sb.ToString();
  36. //Console.WriteLine(pattern);
  37. }
  38. }
  39. else
  40. {
  41. Console.WriteLine("No shake.");
  42. Console.WriteLine(input);
  43. break;
  44. }
  45.  
  46. }
  47.  
  48. }
  49. }
  50. // Надолу още 2 решения:
  51. // Най-оптималната програма:
  52. //string input = Console.ReadLine(); /// Не слагай ToLower
  53. //string pattern = Console.ReadLine(); /// Не слагай ToLower
  54. // while (true)
  55. // {
  56.  
  57. // int indexFirst = input.IndexOf(pattern);
  58. //int indexLast = input.LastIndexOf(pattern);
  59. // if(indexFirst!=indexLast)
  60. // {
  61. // input = input.Remove(indexFirst, pattern.Length);
  62.  
  63.  
  64. // input = input.Remove(input.LastIndexOf(pattern), pattern.Length); //indexLast-pattern.Length
  65.  
  66. // Console.WriteLine("Shaked it.");
  67.  
  68. // pattern = pattern.Remove(pattern.Length / 2, 1);
  69.  
  70. // if (pattern == "")
  71. // {
  72. // Console.WriteLine("No shake.");
  73. // Console.WriteLine(input);
  74. // break;
  75. // }
  76. // }
  77. // else
  78. // {
  79. // Console.WriteLine("No shake.");
  80. // Console.WriteLine(input);
  81. // break;
  82. // }
  83.  
  84. // Treto reshenie:
  85. // StringBuilder optimized:
  86. //string input = Console.ReadLine();
  87. //string pattern = Console.ReadLine();
  88. //while (true)
  89. //{
  90.  
  91. // int indexFirst = input.IndexOf(pattern);
  92. // int indexLast = input.LastIndexOf(pattern);
  93. // if (indexFirst != indexLast)
  94. // {
  95. // StringBuilder sb = new StringBuilder(input);
  96. // sb.Remove(indexFirst, pattern.Length);
  97. // input = sb.ToString();
  98.  
  99. // sb.Remove(input.LastIndexOf(pattern), pattern.Length); //indexLast-pattern.Length
  100. // input = sb.ToString();
  101.  
  102. // Console.WriteLine("Shaked it.");
  103.  
  104. // sb = new StringBuilder(pattern);
  105. // int indexToRemove = pattern.Length / 2;
  106. // sb.Remove(indexToRemove, 1);
  107. // pattern = sb.ToString();
  108. // if (pattern == "")
  109. // {
  110. // Console.WriteLine("No shake.");
  111. // Console.WriteLine(input);
  112. // break;
  113. // }
  114. // }
  115. // else
  116. // {
  117. // Console.WriteLine("No shake.");
  118. // Console.WriteLine(input);
  119. // break;
  120. // }
  121.  
  122. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement