Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package A_TheExam;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- public class P02 {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int length = Integer.parseInt(reader.readLine());
- String input = "";
- int bestIndex = 1;
- int bestLength = 0;
- int bestStartIndex = 0;
- int[] bestSequence = new int[length];
- int currArray = 0;
- while (true) {
- currArray++;
- input = reader.readLine();
- if ("Clone them!".equals(input)) break;
- int[] sequence = new int[length];
- sequence = Arrays.stream(input.split("(!+)"))
- .mapToInt(Integer::parseInt)
- .toArray();
- int currLen = 0;
- int currStartIndex = 0;
- for (int i = 0; i < sequence.length; i++) {
- if (sequence[i] == 1) {
- currLen++;
- currStartIndex = i - currLen + 1;
- } else {
- if (currLen > bestLength) {
- bestLength = currLen;
- bestSequence = sequence;
- bestStartIndex = currStartIndex;
- bestIndex = currArray;
- } else if (currLen == bestLength) {
- if (currStartIndex < bestIndex) { // Should be bestStartIndex!
- bestLength = currLen;
- bestSequence = sequence;
- bestStartIndex = currStartIndex;
- bestIndex = currArray;
- } else if (currStartIndex == bestStartIndex) {
- int bestSum = Arrays.stream(bestSequence).sum();
- int seqSum = Arrays.stream(sequence).sum();
- if (seqSum > bestSum) {
- bestLength = currLen;
- bestSequence = sequence;
- bestStartIndex = currStartIndex;
- bestIndex = currArray;
- }
- }
- }
- currLen = 0;
- }
- }
- if (currLen > bestLength) {
- bestLength = currLen;
- bestSequence = sequence;
- bestStartIndex = currStartIndex;
- bestIndex = currArray;
- }
- }
- System.out.printf("Best DNA sample %d with sum: %d.%n", bestIndex, Arrays.stream(bestSequence).sum());
- for (int i : bestSequence) {
- String str = String.valueOf(i);
- System.out.print(str + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment