Advertisement
TeMePyT

Untitled

May 25th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. int n = int.Parse(Console.ReadLine());
  9. string input = Console.ReadLine();
  10. string[] bestDna = null;
  11. int bestLen = -1;
  12. int startIndex = -1;
  13. int bestDnaSum = 0;
  14. int bestSampleIndex = 0;
  15.  
  16. int currentSampleIndex = 0;
  17.  
  18. while (input != "Clone them!")
  19. {
  20. string[] currentDna = input.Split(new char[] { '!' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  21. int currentLen = 0;
  22. int currentBestLen = 0;
  23. int currentEndIndex = 0;
  24.  
  25. for (int i = 0; i < currentDna.Length; i++)
  26. {
  27. if (currentDna[i] == "1")
  28. {
  29. currentLen++;
  30. if (currentLen > currentBestLen)
  31. {
  32. currentEndIndex = i;
  33. currentBestLen = currentLen;
  34. }
  35. }
  36. else
  37. {
  38. currentLen = 0;
  39. }
  40. }
  41.  
  42. int currentStartIndex = currentEndIndex - currentBestLen + 1;
  43.  
  44. bool isCurrentDnaBetter = false;
  45. int currentDnaSum = currentDna.Select(int.Parse).Sum();
  46.  
  47. if (currentBestLen > bestLen)
  48. {
  49. isCurrentDnaBetter = true;
  50. }
  51. else if (currentBestLen == bestLen)
  52. {
  53. if (currentStartIndex < startIndex)
  54. {
  55. isCurrentDnaBetter = true;
  56. }
  57. else if (currentStartIndex == startIndex)
  58. {
  59. if (currentDnaSum > bestDnaSum)
  60. {
  61. isCurrentDnaBetter = true;
  62. }
  63. }
  64. }
  65.  
  66. currentSampleIndex++;
  67.  
  68. if (isCurrentDnaBetter)
  69. {
  70. bestDna = currentDna;
  71. bestLen = currentBestLen;
  72. startIndex = currentStartIndex;
  73. bestDnaSum = currentDnaSum;
  74. bestSampleIndex = currentSampleIndex;
  75. }
  76. input = Console.ReadLine();
  77. }
  78.  
  79. Console.WriteLine($"Best DNA sample {bestSampleIndex} with sum: {bestDnaSum}.");
  80. Console.WriteLine(string.Join(" ", bestDna));
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement