Advertisement
SvilenVelikov

Kamino Factory

Jun 8th, 2020
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class KaminoFactory {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int lengthSequence = Integer.parseInt(sc.nextLine());
  8.  
  9.         List<String> cloneDatabase = new ArrayList<>();
  10.         int currLength = -1, bestLength = -1;
  11.         int currSumAllONES = 0;
  12.         int currStartIndexLength = -1, startIndexBestLength = -1;
  13.  
  14.  
  15.         String input = sc.nextLine();
  16.         while (!"Clone them!".equals(input)) {
  17.             int[] currArr = Arrays.stream(input.split("!+")).mapToInt(x -> Integer.parseInt(x)).toArray();
  18.  
  19.             for (int i = 0; i < currArr.length; i++) {
  20.                 if (currArr[i] == 1) {
  21.                     currLength = 1;
  22.                     currSumAllONES++;
  23.                     currStartIndexLength = i; //ходи само на единиците
  24.                     for (int j = i + 1; j < currArr.length; j++) {
  25.                         if (currArr[j] == 1) {
  26.                             currSumAllONES++;
  27.                             currLength++;
  28.                             if (j == currArr.length - 1) {
  29.                                 i += currLength;
  30.                                 break;
  31.                             }
  32.                         } else {
  33.                             i += currLength;
  34.                             break;
  35.                         }
  36.                     }
  37.                 }//if-a
  38.  
  39.                 if (currLength > bestLength) {
  40.                     bestLength = currLength;
  41.                     startIndexBestLength = currStartIndexLength;
  42.                 }
  43.             }
  44.  
  45.             int numberOfClone;
  46.             if (cloneDatabase.isEmpty()) {
  47.                 numberOfClone = 1;
  48.             } else {
  49.                 numberOfClone = cloneDatabase.size() + 1;
  50.             }
  51.             String elToAdd = bestLength + " " + startIndexBestLength + " " + currSumAllONES + " " + input
  52.                     + " " + numberOfClone;
  53.             cloneDatabase.add(elToAdd);
  54.  
  55.             currLength = -1;
  56.             bestLength = -1;
  57.             currSumAllONES = 0;
  58.             currStartIndexLength = -1;
  59.             startIndexBestLength = -1;
  60.  
  61.             input = sc.nextLine();
  62.         }
  63.  
  64.         int finalBestLength = -1;
  65.         int finalIndexBestLength = -1;
  66.         int finalcurrSumAllOnes = -1;
  67.  
  68.         cloneDatabase = cloneDatabase.stream()
  69.                 .sorted((x1, x2) -> {
  70.                     String[] a1 = x1.split(" ");
  71.                     String[] a2 = x2.split(" ");
  72.                     int result = Integer.parseInt(a1[0]) - Integer.parseInt(a2[0]);
  73.                     if (result == 0) {
  74.                         result = Integer.parseInt(a2[1]) - Integer.parseInt(a1[1]);
  75.                         if (result == 0) {
  76.                             result = Integer.parseInt(a1[2]) - Integer.parseInt(a2[2]);
  77.                         }
  78.                     }
  79.  
  80.                     return result;
  81.                 })
  82.                 .collect(Collectors.toList());
  83.  
  84.         String[] tokens = cloneDatabase.get(cloneDatabase.size() - 1).split(" ");
  85.  
  86.         System.out.println(String.format("Best DNA sample %s with sum: %s.", tokens[4], tokens[2]));
  87.         System.out.println(tokens[3].replaceAll("!+", " "));
  88.  
  89. //        System.out.println();
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement