Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Problem02
- {
- public class Program
- {
- static void Main(string[] args)
- {
- var dnaLengh = int.Parse(Console.ReadLine());
- var allDna = new List<Dna>();
- string input;
- var counter = 1;
- while (!(input = Console.ReadLine()).Equals("Clone them!"))
- {
- var tokens = input.Split(new[] { '!' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
- if (tokens.Count != dnaLengh)
- {
- continue;
- }
- if (tokens.All(p => p != 1))
- {
- var currentDna1 = new Dna
- {
- DnaSequence = new List<int>(tokens),
- LongestSequence = new KeyValuePair<int, int>(0,-1),
- Sum = tokens.Sum(),
- InputIndex = counter
- };
- allDna.Add(currentDna1);
- counter++;
- continue;
- }
- var currentDna = new Dna
- {
- DnaSequence = new List<int>(tokens),
- LongestSequence = CalculateLongestSequence(tokens),
- Sum = tokens.Sum(),
- InputIndex = counter
- };
- if (currentDna.LongestSequence.Value >= 0)
- {
- allDna.Add(currentDna);
- }
- counter++;
- }
- allDna = allDna.OrderByDescending(p => p.LongestSequence.Key).ThenBy(p => p.LongestSequence.Value)
- .ThenByDescending(p => p.Sum).ToList();
- if (allDna.Any())
- {
- var output = allDna.First();
- Console.WriteLine($"Best DNA sample {output.InputIndex} with sum: {output.Sum}.{Environment.NewLine}{string.Join(" ", output.DnaSequence)}");
- }
- }
- private static KeyValuePair<int, int> CalculateLongestSequence(List<int> tokens)
- {
- var size = 0;
- var currentSize = 0;
- var startIndex = -1;
- for (int i = 0; i < tokens.Count; i++)
- {
- if (tokens[i] == 1)
- {
- currentSize++;
- }
- else
- {
- if (currentSize > size)
- {
- size = currentSize;
- startIndex = i - currentSize;
- }
- currentSize = 0;
- }
- }
- if (currentSize>size)
- {
- size = currentSize;
- startIndex = tokens.Count - size;
- }
- return new KeyValuePair<int, int>(size, startIndex);
- }
- }
- public class Dna
- {
- public List<int> DnaSequence { get; set; }
- public KeyValuePair<int, int> LongestSequence { get; set; }
- public int Sum { get; set; }
- public int InputIndex { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement