Advertisement
emodev

Untitled

Feb 11th, 2019
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package MidExam.Arrays.ArraysExercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class KaminoFactory {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.         int elements = Integer.parseInt(console.nextLine());
  9.  
  10.  
  11.         String bestDna = "";
  12.         int bestLength = 0;
  13.         int bestSum = 0;
  14.         int bestIndex = 0;
  15.         int bestSample = 0;
  16.         int counter = 0;
  17.         String oneSequence = "";
  18.  
  19.         while (true) {
  20.             counter++;
  21.             String input = console.nextLine();
  22.             if (input.equals("Clone them!")) {
  23.                 break;
  24.             }
  25.  
  26.             int maxLength = 0;
  27.             int sum = 0;
  28.  
  29.             String sequence = input.replaceAll("!", "");
  30.             String[] ones = sequence.split("0+");
  31.  
  32.             for (int i = 0; i < ones.length; i++) {
  33.                 if (ones[i].length() > maxLength) {
  34.                     maxLength = ones[i].length();
  35.                     oneSequence = ones[i];
  36.                 }
  37.                 sum += ones[i].length();
  38.             }
  39.             int leftmostOne = sequence.indexOf(oneSequence);
  40.  
  41.  
  42.             if (maxLength > bestLength) {
  43.                 bestLength = maxLength;
  44.                 bestDna = sequence;
  45.                 bestIndex = leftmostOne;
  46.                 bestSum = sum;
  47.                 bestSample = counter;
  48.             } else if (maxLength == bestLength &&
  49.                     (leftmostOne < bestIndex || sum > bestSum)) {
  50.                 bestLength = maxLength;
  51.                 bestDna = sequence;
  52.                 bestIndex = leftmostOne;
  53.                 bestSum = sum;
  54.                 bestSample = counter;
  55.  
  56.             } else if (counter == 1) {
  57.                 bestLength = maxLength;
  58.                 bestSum = sum;
  59.                 bestDna = sequence;
  60.                 bestIndex = leftmostOne;
  61.                 bestSample = counter;
  62.             }
  63.  
  64.         }
  65.         System.out.printf("Best DNA sample %d with sum: %d.%n", bestSample, bestSum);
  66.         String[] result = bestDna.split("");
  67.         System.out.println(String.join(" ", result));
  68.  
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement