veronikaaa86

Evil problem. 2

Mar 3rd, 2018
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package A_TheExam;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7.  
  8. public class P02 {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         int length = Integer.parseInt(reader.readLine());
  13.  
  14.         String input = "";
  15.         int bestIndex = 1;
  16.         int bestLength = 0;
  17.         int bestStartIndex = 0;
  18.         int[] bestSequence = new int[length];
  19.         int currArray = 0;
  20.  
  21.         while (true) {
  22.             currArray++;
  23.             input = reader.readLine();
  24.             if ("Clone them!".equals(input)) break;
  25.  
  26.             int[] sequence = new int[length];
  27.  
  28.             sequence = Arrays.stream(input.split("(!+)"))
  29.                     .mapToInt(Integer::parseInt)
  30.                     .toArray();
  31.  
  32.             int currLen = 0;
  33.             int currStartIndex = 0;
  34.             for (int i = 0; i < sequence.length; i++) {
  35.                 if (sequence[i] == 1) {
  36.                     currLen++;
  37.                     currStartIndex = i - currLen + 1;
  38.                 } else {
  39.                     if (currLen > bestLength) {
  40.                         bestLength = currLen;
  41.                         bestSequence = sequence;
  42.                         bestStartIndex = currStartIndex;
  43.                         bestIndex = currArray;
  44.                     } else if (currLen == bestLength) {
  45.                         if (currStartIndex < bestIndex) { // Should be bestStartIndex!
  46.                             bestLength = currLen;
  47.                             bestSequence = sequence;
  48.                             bestStartIndex = currStartIndex;
  49.                             bestIndex = currArray;
  50.                         } else if (currStartIndex == bestStartIndex) {
  51.                             int bestSum = Arrays.stream(bestSequence).sum();
  52.                             int seqSum = Arrays.stream(sequence).sum();
  53.                             if (seqSum > bestSum) {
  54.                                 bestLength = currLen;
  55.                                 bestSequence = sequence;
  56.                                 bestStartIndex = currStartIndex;
  57.                                 bestIndex = currArray;
  58.                             }
  59.                         }
  60.                     }
  61.                     currLen = 0;
  62.                 }
  63.             }
  64.             if (currLen > bestLength) {
  65.                 bestLength = currLen;
  66.                 bestSequence = sequence;
  67.                 bestStartIndex = currStartIndex;
  68.                 bestIndex = currArray;
  69.             }
  70.         }
  71.  
  72.         System.out.printf("Best DNA sample %d with sum: %d.%n", bestIndex, Arrays.stream(bestSequence).sum());
  73.  
  74.         for (int i : bestSequence) {
  75.             String str = String.valueOf(i);
  76.             System.out.print(str + " ");
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment