Miki1337

Untitled

Feb 19th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 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());//pretty 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
  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();//Converting the Input string to integer array of 1`s and 0`s(Current DNA)
  20. int currentSum = currentDna.Sum();//Sum of the current DNA
  21. int seqCounter = 0; //Counter of 1`s for the current DNA
  22. int bestDnaSeq = 0; //Counter for longest sequence of 1`s in the current DNA
  23. int bestDnaSeqIndexChecker = 0; //Counter of the best sequence index (for the longest sequence of 1`s )in the current DNA
  24. Dna++;//current DNA index
  25.  
  26. for (int i = 0; i < currentDna.Length; i++)
  27. {
  28. if (currentDna[i] == 1)
  29. {
  30. seqCounter++;
  31. if (seqCounter > bestDnaSeq)
  32. {
  33. bestDnaSeq = seqCounter;
  34. bestDnaSeqIndexChecker = (i + 2 - bestDnaSeq);
  35. }
  36. }
  37. else
  38. {
  39. seqCounter = 0;
  40. }
  41. }
  42. if (bestDnaSeq > bestDnaSeqFinal)
  43. {
  44. bestDnaSeqFinal = bestDnaSeq;
  45. finalSum = currentSum;
  46. bestDnaSeqIndex = bestDnaSeqIndexChecker;
  47. result = currentDna;
  48. finalDna = Dna;
  49. }
  50. if (bestDnaSeq == bestDnaSeqFinal && bestDnaSeqIndexChecker < bestDnaSeqIndex)
  51. {
  52. bestDnaSeqIndex = bestDnaSeqIndexChecker;
  53. result = currentDna;
  54. finalDna = Dna;
  55. finalSum = currentSum;
  56. }
  57. if (bestDnaSeq == bestDnaSeqFinal && bestDnaSeqIndexChecker == bestDnaSeqIndex && currentSum > finalSum)
  58. {
  59. finalSum = currentSum;
  60. result = currentDna;
  61. finalDna = Dna;
  62. }
  63. input = Console.ReadLine();
  64.  
  65. }
  66. Console.WriteLine($"Best DNA sample {finalDna} with sum: {finalSum}.");
  67. Console.WriteLine(string.Join(" ", result));
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment