Advertisement
Guest User

Untitled

a guest
Jun 28th, 2018
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _02._Kamino_Factory
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             Regex reg = new Regex(@"1+");
  13.  
  14.             int leftMostIndex = int.MaxValue;
  15.             int bestSequenceLength = 0;
  16.             int bestSum = 0;
  17.             int bestSampleIndex = 0;
  18.             char[] bestSample = new char[n];
  19.  
  20.             int sampleIndex = 1;
  21.             string input = string.Empty;
  22.             while ((input = Console.ReadLine().Replace("!", "")) != "Clone them")
  23.             {
  24.                 string currentBestSequence = string.Empty;
  25.                 foreach (Match match in reg.Matches(input))
  26.                 {
  27.                     if (currentBestSequence.Length < match.Groups[0].Value.Length)
  28.                     {
  29.                         currentBestSequence = match.Groups[0].Value;
  30.                     }
  31.                 }
  32.                 bool foundNewBestSequence = false;
  33.                 if (bestSequenceLength < currentBestSequence.Length)
  34.                 {
  35.                     foundNewBestSequence = true;
  36.                  
  37.                 }
  38.                 else if (bestSequenceLength == currentBestSequence.Length)
  39.                 {
  40.                     if (leftMostIndex > input.IndexOf(currentBestSequence))
  41.                     {
  42.                         foundNewBestSequence = true;
  43.                     }
  44.                     else if (bestSum < input.Replace("0", "").Length)
  45.                     {
  46.                         foundNewBestSequence = true;
  47.                     }
  48.                 }
  49.                 if (foundNewBestSequence)
  50.                 {
  51.                     bestSequenceLength = currentBestSequence.Length;
  52.                     leftMostIndex = input.IndexOf(currentBestSequence);
  53.                     bestSampleIndex = sampleIndex;
  54.                     bestSum = input.Replace("0", "").Length;
  55.                     bestSample = input.ToCharArray();
  56.                 }
  57.                 sampleIndex++;
  58.             }
  59.             Console.WriteLine($"Best DNA sample {bestSampleIndex} with sum: {bestSum}.");
  60.             Console.WriteLine(string.Join(" ", bestSample));
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement