Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 9.*Kamino Factory
- The clone factory in Kamino got another order to clone troops. But this time you are tasked to find the best DNA sequence to use in the production.
- You will receive the DNA length and until you receive the command "Clone them!" you will be receiving a DNA sequences of ones and zeroes, split by "!" (one or several).
- You should select the sequence with the longest subsequence of ones. If there are several sequences with same length of subsequence of ones, print the one with the leftmost starting index, if there are several sequences with same length and starting index, select the sequence with the greater sum of its elements.
- After you receive the last command "Clone them!" you should print the collected information in the following format:
- "Best DNA sample {bestSequenceIndex} with sum: {bestSequenceSum}."
- "{DNA sequence, joined by space}"
- Input / Constraints
- • The first line holds the length of the sequences – integer in range [1…100];
- • On the next lines until you receive "Clone them!" you will be receiving sequences (at least one) of ones and zeroes, split by "!" (one or several).
- Output
- The output should be printed on the console and consists of two lines in the following format:
- "Best DNA sample {bestSequenceIndex} with sum: {bestSequenceSum}."
- "{DNA sequence, joined by space}"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _09KaminoFactory
- {
- class Program
- {
- static void Main(string[] args)
- {
- int arrayLenght = int.Parse(Console.ReadLine());
- string input = Console.ReadLine();
- int[] DNA = new int[arrayLenght];
- int lenght = 0;
- int index = 0;
- int sum = 0;
- int currentRow = 0;
- int row = 0;
- while (input != "Clone them!")
- {
- int[] currentDNA = input.Split("!", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- currentRow++;
- int currentSum = 0;
- for (int i = 0; i < currentDNA.Length; i++)
- {
- if (currentDNA[i] == 1)
- {
- currentSum++;
- }
- }
- int currentLenght = 0;
- int currentIndex = 0;
- for (int i = 0; i < currentDNA.Length; i++)
- {
- if (currentDNA[i] == 1)
- {
- currentLenght++;
- if (currentLenght == 1)
- {
- currentIndex = i;
- }
- if (currentLenght > lenght || currentLenght == lenght && (currentIndex < index || currentSum > sum))
- {
- lenght = currentLenght;
- index = currentIndex;
- row = currentRow;
- DNA = currentDNA;
- sum = currentSum;
- }
- }
- else
- {
- currentIndex = 0;
- currentLenght = 0;
- }
- }
- input = Console.ReadLine();
- }
- if (row == 0)
- {
- row = 1;
- }
- Console.WriteLine($"Best DNA sample {row} with sum: {sum}.");
- Console.WriteLine(string.Join(" ", DNA));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment