Advertisement
IvanBorisovG

KaminoFactoryRegex

Mar 14th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8. using System.Numerics;
  9.  
  10. namespace Exam
  11. {
  12.     class Program
  13.     {
  14.              static void Main(string[] args)
  15.         {
  16.             var n = int.Parse(Console.ReadLine());
  17.  
  18.             var input = "";
  19.             var pattern = @"(1+)";
  20.             var regex = new Regex(pattern);
  21.             var sum = 0;
  22.             var index = int.MaxValue;
  23.             var length = 0;
  24.             var result = "";
  25.             var row = 1;
  26.             var currentRow = 0;
  27.  
  28.             while ((input = Console.ReadLine()) != "Clone them!")
  29.             {
  30.                 var splitInput = input.Split(new char[] { '!' },StringSplitOptions.RemoveEmptyEntries);
  31.                 var seq = string.Join("", splitInput);
  32.                 var currentSum = 0;
  33.                 var currentIndex = 0;
  34.                 var currentLength = 0;
  35.                 var find = "";
  36.                 currentRow++;
  37.  
  38.                 var matches = regex.Matches(seq);
  39.  
  40.                 foreach (Match match in matches)
  41.                 {
  42.                     if (match.Length > currentLength)
  43.                     {
  44.                         currentLength = match.Length;
  45.                         find = match.Groups[1].Value;
  46.                     }
  47.  
  48.                     currentSum += match.Length;
  49.                 }
  50.  
  51.                 currentIndex = seq.IndexOf(find);
  52.  
  53.                 if (currentLength > length)
  54.                 {
  55.                     index = currentIndex;
  56.                     length = currentLength;
  57.                     sum = currentSum;
  58.                     result = seq;
  59.                     row = currentRow;
  60.                 }
  61.                 else if (currentLength == length)
  62.                 {
  63.                     if (currentIndex < index)
  64.                     {
  65.                         index = currentIndex;
  66.                         length = currentLength;
  67.                         sum = currentSum;
  68.                         result = seq;
  69.                         row = currentRow;
  70.                     }
  71.                     else if (currentIndex==index)
  72.                     {
  73.                         if (currentSum>sum)
  74.                         {
  75.                             index = currentIndex;
  76.                             length = currentLength;
  77.                             sum = currentSum;
  78.                             result = seq;
  79.                             row = currentRow;
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             Console.WriteLine($"Best DNA sample {row} with sum: {sum}.");
  86.             foreach (var res in result)
  87.             {
  88.                 Console.Write(res + " ");
  89.             }
  90.  
  91.             Console.WriteLine();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement