Advertisement
elvirynwa

Kamino Factory

Mar 22nd, 2019
1,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class KaminoFactory {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int size = Integer.parseInt(scanner.nextLine());
  8.  
  9.         String input = "";
  10.         int bestSequenceIndex = Integer.MAX_VALUE;
  11.         int bestSequenceSum = 0;
  12.         int bestIndexInARow = 0;
  13.         int bestIndexInARowOutput = 0;
  14.         String sequenceOutput = "";
  15.  
  16.  
  17.         while (!"Clone them!".equals(input = scanner.nextLine())) {
  18.             ++bestIndexInARow;
  19.  
  20.             String[] data = input.split("!+");
  21.  
  22.             int[] sequenceDNA = new int[size];
  23.  
  24.             for (int i = 0; i < data.length; i++) {
  25.                 sequenceDNA[i] = Integer.parseInt(data[i]);
  26.             }
  27.  
  28.             int maxCount = 0;
  29.             int sequenceIndex = 0;
  30.             for (int i = 0; i < sequenceDNA.length; i++) {
  31.                 int currentCount = 0;
  32.                 for (int j = i; j < sequenceDNA.length; j++) {
  33.                     if (sequenceDNA[i] == sequenceDNA[j]) {
  34.                         currentCount++;
  35.                         if (currentCount > maxCount) {
  36.                             maxCount = currentCount;
  37.                             sequenceIndex = i;
  38.                         }
  39.                     } else {
  40.                         break;
  41.                     }
  42.                 }
  43.             }
  44.             int sequenceSum = 0;
  45.  
  46.             for (int i = 0; i < sequenceDNA.length; i++) {
  47.  
  48.                 if (sequenceDNA[i] == 1) {
  49.                     sequenceSum += sequenceDNA[i];
  50.                 }
  51.             }
  52.             if (sequenceIndex < bestSequenceIndex || sequenceSum > bestSequenceSum) {
  53.                 sequenceOutput = "";
  54.                 bestSequenceIndex = sequenceIndex;
  55.                 bestSequenceSum = sequenceSum;
  56.                 bestIndexInARowOutput = bestIndexInARow;
  57.  
  58.                 for (int i = 0; i < sequenceDNA.length; i++) {
  59.                     sequenceOutput += sequenceDNA[i] + " ";
  60.                 }
  61.             }
  62.         }
  63.         System.out.println(String.format("Best DNA sample %d with sum: %d.", bestIndexInARowOutput, bestSequenceSum));
  64.         System.out.println(sequenceOutput.trim());
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement