Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class KaminoFactory {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int lengthSequence = Integer.parseInt(sc.nextLine());
- List<String> cloneDatabase = new ArrayList<>();
- int currLength = -1, bestLength = -1;
- int currSumAllONES = 0;
- int currStartIndexLength = -1, startIndexBestLength = -1;
- String input = sc.nextLine();
- while (!"Clone them!".equals(input)) {
- int[] currArr = Arrays.stream(input.split("!+")).mapToInt(x -> Integer.parseInt(x)).toArray();
- for (int i = 0; i < currArr.length; i++) {
- if (currArr[i] == 1) {
- currLength = 1;
- currSumAllONES++;
- currStartIndexLength = i; //ходи само на единиците
- for (int j = i + 1; j < currArr.length; j++) {
- if (currArr[j] == 1) {
- currSumAllONES++;
- currLength++;
- if (j == currArr.length - 1) {
- i += currLength;
- break;
- }
- } else {
- i += currLength;
- break;
- }
- }
- }//if-a
- if (currLength > bestLength) {
- bestLength = currLength;
- startIndexBestLength = currStartIndexLength;
- }
- }
- int numberOfClone;
- if (cloneDatabase.isEmpty()) {
- numberOfClone = 1;
- } else {
- numberOfClone = cloneDatabase.size() + 1;
- }
- String elToAdd = bestLength + " " + startIndexBestLength + " " + currSumAllONES + " " + input
- + " " + numberOfClone;
- cloneDatabase.add(elToAdd);
- currLength = -1;
- bestLength = -1;
- currSumAllONES = 0;
- currStartIndexLength = -1;
- startIndexBestLength = -1;
- input = sc.nextLine();
- }
- int finalBestLength = -1;
- int finalIndexBestLength = -1;
- int finalcurrSumAllOnes = -1;
- cloneDatabase = cloneDatabase.stream()
- .sorted((x1, x2) -> {
- String[] a1 = x1.split(" ");
- String[] a2 = x2.split(" ");
- int result = Integer.parseInt(a1[0]) - Integer.parseInt(a2[0]);
- if (result == 0) {
- result = Integer.parseInt(a2[1]) - Integer.parseInt(a1[1]);
- if (result == 0) {
- result = Integer.parseInt(a1[2]) - Integer.parseInt(a2[2]);
- }
- }
- return result;
- })
- .collect(Collectors.toList());
- String[] tokens = cloneDatabase.get(cloneDatabase.size() - 1).split(" ");
- System.out.println(String.format("Best DNA sample %s with sum: %s.", tokens[4], tokens[2]));
- System.out.println(tokens[3].replaceAll("!+", " "));
- // System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement