Advertisement
WindFell

Kamino Factory

Mar 15th, 2018
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class KaminoFactory
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         int sequenceLength = int.Parse(Console.ReadLine());
  10.         string sequence = Console.ReadLine();
  11.  
  12.         List<int[]> samples = new List<int[]>();
  13.  
  14.         while (sequence != "Clone them!")
  15.         {
  16.             int[] dna = sequence
  17.                 .Split("!".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  18.                 .Select(int.Parse)
  19.                 .ToArray();
  20.  
  21.             if (dna.Length == sequenceLength)
  22.             {
  23.                 samples.Add(dna);
  24.             }
  25.  
  26.             sequence = Console.ReadLine();
  27.         }
  28.  
  29.         int index = 1;
  30.         int bestStartIndex = sequenceLength;
  31.         int bestLength = 0;
  32.         int bestSum = 0;
  33.         string bestSample = string.Join(" ", samples[0]);
  34.  
  35.         for (int sequenceIndex = 0; sequenceIndex < samples.Count; sequenceIndex++)
  36.         {
  37.             int sum = samples[sequenceIndex].Sum();
  38.             int startIndex = sequenceLength;
  39.             int bestLen = 0;
  40.             int len = 0;
  41.  
  42.             for (int i = 0; i < samples[sequenceIndex].Length; i++)
  43.             {
  44.                 if (samples[sequenceIndex][i] == 1)
  45.                 {
  46.                     len++;
  47.                 }
  48.                 else
  49.                 {
  50.                     if (len > bestLen)
  51.                     {
  52.                         bestLen = len;
  53.                         startIndex = i - len;
  54.                     }
  55.  
  56.                     len = 0;
  57.                 }
  58.             }
  59.  
  60.             if (bestLen > bestLength)
  61.             {
  62.                 bestLength = bestLen;
  63.                 index = sequenceIndex + 1;
  64.                 bestSum = sum;
  65.                 bestStartIndex = startIndex;
  66.                 bestSample = string.Join(" ", samples[sequenceIndex]);
  67.             }
  68.             else if (bestLen == bestLength)
  69.             {
  70.                 if (bestStartIndex > startIndex)
  71.                 {
  72.                     index = sequenceIndex + 1;
  73.                     bestSum = sum;
  74.                     bestStartIndex = startIndex;
  75.                     bestSample = string.Join(" ", samples[sequenceIndex]);
  76.                 }
  77.                 else if (bestStartIndex == startIndex)
  78.                 {
  79.                     if (bestSum < sum)
  80.                     {
  81.                         index = sequenceIndex + 1;
  82.                         bestSum = sum;
  83.                         bestSample = string.Join(" ", samples[sequenceIndex]);
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.  
  89.         Console.WriteLine($"Best DNA sample {index} with sum: {bestSum}.");
  90.         Console.WriteLine($"{bestSample}");
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement