Advertisement
Guest User

Untitled

a guest
May 14th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. namespace P09_KaminoFactory
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class P09_KaminoFactory
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int sequenceLength = int.Parse(Console.ReadLine());
  11.             string input = Console.ReadLine();
  12.  
  13.             int[] DNA = new int[sequenceLength];
  14.             int DNASum = 0;
  15.             int DNACount = 0;
  16.             int DNASample = 0;
  17.  
  18.             int sample = 0;
  19.             while (input != "Clone them!")
  20.             {
  21.                 //----------------------------- CURRENT DNA INFO -----------------------------
  22.                 sample++;
  23.                 int[] currDNA = input.Split("!", StringSplitOptions.RemoveEmptyEntries)
  24.                                     .Select(int.Parse)
  25.                                     .ToArray();
  26.                 int sum = 0;
  27.                 int currCount = 0;
  28.                 int count = 0;
  29.  
  30.                 for (int i = currDNA.Length - 1; i >= 0; i--)
  31.                 {
  32.                     if (currDNA[i] != 1)
  33.                     {
  34.                         count = 0;
  35.                         continue;
  36.                     }
  37.                     sum++;
  38.  
  39.                     count++;
  40.                     if (count >= currCount)
  41.                     {
  42.                         currCount = count;
  43.                     }
  44.                 }
  45.                 //----------------------------------------------------------------------------            
  46.                 //------------------ Check CURRENT DNA with BEST DNA -------------------------
  47.                 if (currCount > DNACount)
  48.                 {
  49.                     DNACount = currCount;
  50.                     DNASum = sum;
  51.                     DNA = currDNA;
  52.                     DNASample = sample;
  53.                 }
  54.                 else if (currCount == DNACount)
  55.                 {
  56.                     int count1 = 0;
  57.                     int count2 = 0;
  58.  
  59.                     bool isDNAFirst = false;
  60.                     bool isCurrDNAFirst = false;
  61.  
  62.                     //---------------- Find the leftmost best DNA ----------------------------
  63.                     for (int i = 0; i < sequenceLength; i++)
  64.                     {
  65.                         //--------- Best DNA ----------
  66.                         if (DNA[i] == 1)
  67.                         {
  68.                             count1++;
  69.                             if (count1 == DNACount)
  70.                             {
  71.                                 isDNAFirst = true;
  72.                             }
  73.                         }
  74.                         else
  75.                         {
  76.                             count1 = 0;
  77.                         }
  78.  
  79.                         //------- Current DNA ---------
  80.                         if (currDNA[i] == 1)
  81.                         {
  82.                             count2++;
  83.                             if (count2 == currCount)
  84.                             {
  85.                                 isCurrDNAFirst = true;
  86.                             }
  87.                         }
  88.                         else
  89.                         {
  90.                             count2 = 0;
  91.                         }
  92.  
  93.                         //-----------------------------
  94.                         if (isDNAFirst || isCurrDNAFirst)
  95.                         {
  96.                             break;
  97.                         }
  98.                     }
  99.  
  100.                     //--------- IF BOTH ARE EQUAL CHECK THEIR SUMS ---------
  101.                     if (isDNAFirst && isCurrDNAFirst)
  102.                     {
  103.                         if (sum > DNASum)
  104.                         {
  105.                             // current DNA is better
  106.                             DNA = currDNA;
  107.                             DNASum = sum;
  108.                             DNACount = currCount;
  109.                             DNASample = sample;
  110.                         }
  111.                     }
  112.                     //--------- IF CURRENT DNA IS BETTER -------------------
  113.                     else if (isCurrDNAFirst)
  114.                     {
  115.                         DNA = currDNA;
  116.                         DNASum = sum;
  117.                         DNACount = currCount;
  118.                         DNASample = sample;
  119.                     }
  120.                 }
  121.  
  122.                 input = Console.ReadLine();
  123.             }
  124.  
  125.             Console.WriteLine($"Best DNA sample {DNASample} with sum: {DNASum}.");
  126.             Console.WriteLine(String.Join(" ", DNA));
  127.         }    
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement