Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Problem_2___Command_Interpreter
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- static void Main(string[] args)
- {
- int Lenght = int.Parse(Console.ReadLine());
- string read = Console.ReadLine();
- string[] sequence = new string[Lenght];
- List<string[]> Memory = new List<string[]>();
- string[] select = new string[Lenght]; var ones = 0; var longest = 0; var longestsubsequence = new Dictionary<int, int>(); var sample = -1;
- var leftindexsubsequence = new Dictionary<int, int>();
- var LeftIndex = 0;
- var bestSum = 0;
- var sum = 0;
- while (read != "Clone them!")
- {
- sequence = read.Split(new char[] { '!' }, StringSplitOptions.RemoveEmptyEntries);
- Memory.Add(sequence);
- sample++;
- FindingTheLongestSubsequenceOfOnce(sequence, ref ones, ref longest, longestsubsequence, sample);
- if ((longestsubsequence.Values.Last().Equals(longestsubsequence.Values.First()) && longestsubsequence.Count > 1) || (longestsubsequence.Count>2 && longestsubsequence.Values.ElementAt(0)==longestsubsequence.Values.ElementAt(1)) || (longestsubsequence.Count > 2 && longestsubsequence.Values.ElementAt(0) == longestsubsequence.Values.ElementAt(2)) || (longestsubsequence.Count > 2 && longestsubsequence.Values.ElementAt(1) == longestsubsequence.Values.ElementAt(2)) )
- {
- LeftIndex = FindingTheMostLeftIndexSubsequence(Memory, longestsubsequence, leftindexsubsequence, LeftIndex);
- }
- else if (longestsubsequence.Count>1)
- {
- var mostlongestONES = longestsubsequence.Values.OrderByDescending(x => x).Take(1).First();
- for (int i = 0; i < longestsubsequence.Count; i++)
- {
- if (longestsubsequence.Values.ElementAt(i) < mostlongestONES)
- {
- longestsubsequence.Remove(i);
- }
- }
- }
- if (leftindexsubsequence.Count > 1)
- {
- var mostLeftindex = leftindexsubsequence.Values.OrderBy(x => x).Take(1).First();
- for (int i = 0; i < leftindexsubsequence.Count; i++)
- {
- if (leftindexsubsequence.Values.ElementAt(i) > mostLeftindex)
- {
- leftindexsubsequence.Remove(i);
- }
- }
- }
- read = Console.ReadLine();
- }
- if (leftindexsubsequence.Count>1)
- {
- foreach (var item in leftindexsubsequence.Keys)
- {
- for (int i = 0; i < Memory[item].Length; i++)
- {
- if (Memory[item][i]=="1")
- {
- sum++;
- }
- }
- if (sum>bestSum)
- {
- bestSum = sum;
- select = Memory[item];
- sample = item + 1;
- }
- sum = 0;
- }
- }
- else if (leftindexsubsequence.Count==1)
- {
- for (int i = 0; i < Memory[leftindexsubsequence.First().Key].Length; i++)
- {
- if (Memory[leftindexsubsequence.First().Key][i]=="1")
- {
- bestSum++;
- }
- }
- select = Memory[leftindexsubsequence.First().Key];
- sample = leftindexsubsequence.First().Key + 1;
- }
- if (longestsubsequence.Count == 1)
- {
- for (int i = 0; i < Memory[longestsubsequence.First().Key].Length; i++)
- {
- if (Memory[longestsubsequence.First().Key][i] == "1")
- {
- bestSum++;
- }
- }
- select = Memory[longestsubsequence.First().Key];
- sample = longestsubsequence.First().Key + 1;
- }
- Console.WriteLine($"Best DNA sample {sample} with sum: {bestSum}.");
- Console.WriteLine(string.Join(" ", select));
- }
- private static int FindingTheMostLeftIndexSubsequence(List<string[]> Memory, Dictionary<int, int> longestsubsequence, Dictionary<int, int> leftindexsubsequence, int LeftIndex)
- {
- foreach (var item in longestsubsequence.Keys)
- {
- for (int i = 0; i < Memory[item].Length; i++)
- {
- if (Memory[item][i] == "1")
- {
- LeftIndex = i;
- if (i > 0)
- {
- if (!leftindexsubsequence.ContainsValue(LeftIndex) && (Memory[item][i - 1] == "1") && i > 0)
- {
- leftindexsubsequence[item] = LeftIndex - 1;
- }
- else if (Memory[item][i - 1] == "1")
- {
- leftindexsubsequence[item] = LeftIndex - 1;
- }
- }
- }
- }
- }
- return LeftIndex;
- }
- private static void FindingTheLongestSubsequenceOfOnce(string[] sequence, ref int ones, ref int longest, Dictionary<int, int> longestsubsequence, int sample)
- {
- ones = 0;
- for (int i = 0; i < sequence.Length; i++)
- {
- if (sequence[i] == "1")
- {
- ones++;
- }
- else
- {
- ones = 0;
- }
- if (ones >= longest)
- {
- longest = ones;
- if (!longestsubsequence.ContainsValue(longest))
- {
- longestsubsequence[sample] = longest;
- }
- else
- {
- longestsubsequence[sample] = longest;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement