Advertisement
mirozspace

K_Factory_100

Feb 27th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Pr09KaminoFactory {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int sequenceLehgth = Integer.parseInt(scanner.nextLine());
  8.         String line = scanner.nextLine();
  9.  
  10.         int bestLength = 0;
  11.         String bestDna = "";
  12.         int bestIndex = 0;
  13.         int bestSum = 0;
  14.         int counter = 0;
  15.         int bestCounter = 0;
  16.         while (!line.equals("Clone them!")) {
  17.  
  18.             counter++;
  19.  
  20.             String sequence = line.replaceAll("!", "");
  21.             String[] dnas = sequence.split("0");
  22.             int maxLength = 0;
  23.             int sum = 0;
  24.             String bestLocalDna = "";
  25.             for (int i = 0; i < dnas.length; i++) {
  26.                 if (dnas[i].length() > maxLength) {
  27.                     maxLength = dnas[i].length();
  28.                     bestLocalDna = dnas[i];
  29.                 }
  30.                 sum += dnas[i].length();
  31.             }
  32.             int beginIndex = sequence.indexOf(bestLocalDna);
  33.             if (maxLength > bestLength) {
  34.                 bestLength = maxLength;
  35.                 bestDna = sequence;
  36.                 bestIndex = beginIndex;
  37.                 bestSum = sum;
  38.                 bestCounter = counter;
  39.             } else if (maxLength == bestLength && (beginIndex < bestIndex || sum > bestSum)) {
  40.                 bestLength = maxLength;
  41.                 bestDna = sequence;
  42.                 bestIndex = beginIndex;
  43.                 bestSum = sum;
  44.                 bestCounter = counter;
  45.             } else if (counter == 1) {
  46.                 bestLength = maxLength;
  47.                 bestDna = sequence;
  48.                 bestIndex = beginIndex;
  49.                 bestSum = sum;
  50.                 bestCounter = counter;
  51.             }
  52.             line = scanner.nextLine();
  53.         }
  54.         System.out.println(String.format(("Best DNA sample " + "%d with sum: %d."), bestCounter, bestSum));
  55.         for (int i = 0; i < bestDna.length(); i++) {
  56.             System.out.print(bestDna.charAt(i) + " ");
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement