MartinPaunov

Kamino Factory

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