Advertisement
Miki1337

Untitled

Feb 19th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. int dnaLength = int.Parse(Console.ReadLine()); // Useless in this solution
  9. string input = Console.ReadLine(); // String of 1s and 0s separated by ! or !`s
  10. int bestDnaSeqIndex = int.MaxValue; // Longest sequence index
  11. int bestDnaSeqFinal = 0; // Longest sequence length
  12. int finalSum = 0; // Sum of the best DNA
  13. int[] result = new int[dnaLength]; // Best DNA to print
  14. int finalDna = 0; // Best DNA index
  15. int Dna = 0; // DNA index counter
  16.  
  17. while (input != "Clone them!")
  18. {
  19. int[] currentDna = input.Split(new char[] { '!' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  20. //Converting the Input string to integer array of 1`s and 0`s(Current DNA)
  21. int currentSum = currentDna.Sum();
  22. //Sum of the current DNA
  23. int seqCounter = 0;
  24. //Counter of 1`s for the current DNA
  25. int bestDnaSeq = 0;
  26. //Counter for longest sequence of 1`s in the current DNA
  27. int bestDnaSeqIndexChecker = 0;
  28. //Counter of the best sequence index (for the longest sequence of 1`s )in the current DNA
  29. Dna++;
  30. //current DNA index
  31.  
  32. for (int i = 0; i < currentDna.Length; i++)
  33. {
  34. if (currentDna[i] == 1)
  35. {
  36. seqCounter++;
  37. if (seqCounter > bestDnaSeq)
  38. {
  39. bestDnaSeq = seqCounter;
  40. bestDnaSeqIndexChecker = (i + 2 - bestDnaSeq);
  41. }
  42. }
  43. else
  44. {
  45. seqCounter = 0;
  46. }
  47. }
  48. if (bestDnaSeq > bestDnaSeqFinal)
  49. {
  50. bestDnaSeqFinal = bestDnaSeq;
  51. finalSum = currentSum;
  52. bestDnaSeqIndex = bestDnaSeqIndexChecker;
  53. result = currentDna;
  54. finalDna = Dna;
  55. }
  56. if (bestDnaSeq == bestDnaSeqFinal && bestDnaSeqIndexChecker < bestDnaSeqIndex)
  57. {
  58. bestDnaSeqIndex = bestDnaSeqIndexChecker;
  59. result = currentDna;
  60. finalDna = Dna;
  61. finalSum = currentSum;
  62. }
  63. if (bestDnaSeq == bestDnaSeqFinal && bestDnaSeqIndexChecker == bestDnaSeqIndex && currentSum > finalSum)
  64. {
  65. finalSum = currentSum;
  66. result = currentDna;
  67. finalDna = Dna;
  68. }
  69. input = Console.ReadLine();
  70.  
  71. }
  72. Console.WriteLine($"Best DNA sample {finalDna} with sum: {finalSum}.");
  73. Console.WriteLine(string.Join(" ", result));
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement