Runtime error на *Kamino Factory
Колеги, пробвах по три различни начина да реша задачата. И трите пъти ми дава Runtime error на поне половината отговори.
Прилагам и трите варианта на решението.
условието на задачата:
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}"
Examples
Input Output Comments
5
1!0!1!1!0
0!1!1!0!0
Clone them!
Best DNA sample 2 with sum: 2.
0 1 1 0 0
We receive 2 sequences with same length of
subsequence of ones, but the second is printed,
because its subsequence starts at index[1].
Input Output Comments
4
1!1!0!1
1!0!0!1
1!1!0!0
Clone them!
Best DNA sample 1 with sum: 3.
1 1 0 1
We receive 3 sequences. Both 1 and 3 have same
length of subsequence of ones -> 2, and both start
from index[0], but the first is printed, because its sum
is greater.
Линк към джъджа: https://judge.softuni.bg/Contests/Practice/Index/1206#8
1. Вариант на решението
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
int[] maxSeries = new int[n];
int maxSequense = 0;
int maxIndexPosition = n - 1;
int maxSumOfLine = 0;
int lineMax = 0;
int line = 0;
while (true)
{
string input = Console.ReadLine();
if (input == "Clone them!")
{
break;
}
line++;
int[] series = input
.Split('!')
.Select(int.Parse)
.ToArray();
int sequense = 0;
int indexPosition = n-1;
int sumOfLine = 0;
int currentSequense = 0;
for (int i = n - 1; i >= 0; i--)
{
if (series[i] == 1)
{
sumOfLine++;
currentSequense++;
if (currentSequense >= sequense)
{
sequense = currentSequense;
indexPosition = i;
}
}
else
{
currentSequense = 0;
}
} // end for
bool currentlineBestOfMaxLine = IsCurrentlineBestOfMaxLine
(maxSequense, maxIndexPosition, maxSumOfLine, sequense, indexPosition, sumOfLine);
if (currentlineBestOfMaxLine)
{
maxSequense = sequense;
maxIndexPosition = indexPosition;
maxSumOfLine = sumOfLine;
maxSeries = series;
lineMax = line;
}
} // end while
Console.WriteLine("Best DNA sample {0} with sum: {1}.", lineMax, maxSumOfLine);
Console.WriteLine(string.Join(" ", maxSeries));
Console.WriteLine();
}
public static bool IsCurrentlineBestOfMaxLine
(int maxSequense, int maxIndexPosition, int maxSumOfLine, int sequense, int indexPosition, int sumOfLine)
{
if (maxSequense < sequense)
{
return true;
}
else if (maxSequense == sequense && maxIndexPosition > indexPosition)
{
return true;
}
else if (maxSequense == sequense && maxIndexPosition == indexPosition && maxSumOfLine < sumOfLine)
{
return true;
}
else
{
return false;
}
}
}
2. Втори вариант - с използване на лист, за да избегна презаписването на масиви.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int sequenceLingth = int.Parse(Console.ReadLine());
string dna = Console.ReadLine();
List<int[]> allDNA = new List<int[]>();
//int line = 0;
while (dna != "Clone them!")
{
/// line++;
int[] series = dna
.Split('!')
.Select(int.Parse)
.ToArray();
if (series.Length == sequenceLingth)
{
allDNA.Add(series);
}
dna = Console.ReadLine();
} // end while
int maxSequense = 0;
int maxIndexPosition = sequenceLingth - 1;
int maxSumOfLine = 0;
int lineMax = 0;
for (int lineDna = 0; lineDna < allDNA.Count; lineDna++)
{
int sequense = 0;
int indexPosition = sequenceLingth-1;
int sumOfLine = 0;
int currentSequense = 0;
for (int i = allDNA[lineDna].Length - 1; i >= 0; i--)
{
if (allDNA[lineDna][i] == 1)
{
sumOfLine++;
currentSequense++;
}
else
{
if (currentSequense >= sequense)
{
sequense = currentSequense;
indexPosition = i;
}
currentSequense = 0;
}
} // end internal for
bool currentlineBestOfMaxLine = IsCurrentlineBestOfMaxLine
(maxSequense, maxIndexPosition, maxSumOfLine, sequense, indexPosition, sumOfLine);
if (currentlineBestOfMaxLine)
{
maxSequense = sequense;
maxIndexPosition = indexPosition;
maxSumOfLine = sumOfLine;
lineMax = lineDna;
}
} // end external for
Console.WriteLine("Best DNA sample {0} with sum: {1}.", lineMax +1, maxSumOfLine);
Console.WriteLine(string.Join(" ", allDNA[lineMax]));
}
public static bool IsCurrentlineBestOfMaxLine
(int maxSequense, int maxIndexPosition, int maxSumOfLine, int sequense, int indexPosition, int sumOfLine)
{
if (maxSequense < sequense)
{
return true;
}
else if (maxSequense == sequense)
{
if (maxIndexPosition > indexPosition)
{
return true;
}
else if (maxIndexPosition == indexPosition)
{
if (maxSumOfLine < sumOfLine)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
}
3. Трети вариант - без буул и метод, за да облекча пресмятанията на процесора
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int sequenceLingth = int.Parse(Console.ReadLine());
string dna = Console.ReadLine();
List<int[]> allDNA = new List<int[]>();
//int line = 0;
while (dna != "Clone them!")
{
/// line++;
int[] series = dna
.Split('!')
.Select(int.Parse)
.ToArray();
if (series.Length == sequenceLingth)
{
allDNA.Add(series);
}
dna = Console.ReadLine();
} // end while
int maxSequense = 0;
int maxIndexPosition = sequenceLingth - 1;
int maxSumOfLine = 0;
int lineMax = 0;
for (int lineDna = 0; lineDna < allDNA.Count; lineDna++)
{
int sequense = 0;
int indexPosition = sequenceLingth-1;
int sumOfLine = 0;
int currentSequense = 0;
for (int i = allDNA[lineDna].Length - 1; i >= 0; i--)
{
if (allDNA[lineDna][i] == 1)
{
sumOfLine++;
currentSequense++;
}
else
{
if (currentSequense >= sequense)
{
sequense = currentSequense;
indexPosition = i;
}
currentSequense = 0;
}
} // end internal for
if (maxSequense < sequense)
{
maxSequense = sequense;
maxIndexPosition = indexPosition;
maxSumOfLine = sumOfLine;
lineMax = lineDna;
}
else if (maxSequense == sequense)
{
if (maxIndexPosition > indexPosition)
{
maxSequense = sequense;
maxIndexPosition = indexPosition;
maxSumOfLine = sumOfLine;
lineMax = lineDna;
}
else if (maxIndexPosition == indexPosition)
{
if (maxSumOfLine < sumOfLine)
{
maxSequense = sequense;
maxIndexPosition = indexPosition;
maxSumOfLine = sumOfLine;
lineMax = lineDna;
}
}
}
} // end external for
Console.WriteLine("Best DNA sample {0} with sum: {1}.", lineMax +1, maxSumOfLine);
Console.WriteLine(string.Join(" ", allDNA[lineMax]));
}
}