View difference between Paste ID: xL5QrW1X and UDbrVSvv
SHOW: | | - or go back to the newest paste.
1
package ArraysExercise;
2
3
import java.util.Arrays;
4
import java.util.Scanner;
5
6
public class kaminando {
7
    public static void main(String[] args) {
8
        Scanner scanner = new Scanner(System.in);
9
10
        int lengthOfSequence = Integer.parseInt(scanner.nextLine());
11
12
        int countRows = 0;
13
        int bestCountRows = 0;
14
        int bestCountOnes = 0;
15
        int countSequence = 0;
16
        int firstIndex = 0;
17
        int bestIndex = Integer.MAX_VALUE;
18
        int bestSequence = 0;
19
        String[] bestArray = new String[lengthOfSequence];
20
21
        while (true) {
22
            String input = scanner.nextLine();
23
            if (input.equals("Clone them!")) {
24
                break;
25
            }
26
            countRows++;
27
            int countOnes = 0;
28
            String[] DNA = input.split("!+");
29
            for (int i = 0; i < DNA.length; i++) {
30
                int currentCountSequence = 0;
31
                int currentFirstIndex = -1;
32
                int currentCountOnes = 0;
33
34
                if (DNA[i].equals("1")) {
35
                    currentCountOnes++;
36
                }
37
38
39
                for (int j = i + 1; j < DNA.length; j++) {
40
                    if (DNA[i].equals("1") && DNA[j].equals("1") && currentFirstIndex == -1) {
41
                        currentFirstIndex = i;
42
                    }
43
                    if (DNA[i].equals("1") && DNA[j].equals("1")) {
44
                        currentCountSequence++;
45
46
                    } else {
47
                        break;
48
                    }
49
                }
50
                if (currentCountSequence > countSequence) {
51
                    firstIndex = currentFirstIndex;
52
                    countSequence = currentCountSequence;
53
                    bestCountRows = countRows;
54
55
                }
56
                if (currentCountSequence == countSequence && firstIndex > currentFirstIndex){
57
                    firstIndex = currentFirstIndex;
58
                    countSequence = currentCountSequence;
59
                    bestCountRows = countRows;
60
                }
61
                countOnes += currentCountOnes;
62
            }
63
64
65
            if (countSequence > bestSequence) {
66
                bestIndex = firstIndex;
67
                bestSequence = countSequence;
68
                bestArray = Arrays.copyOf(DNA, DNA.length);
69
                bestCountOnes = countOnes;
70
71
72
73
            }
74
            if (countSequence == bestSequence && firstIndex < bestIndex) {
75
                bestIndex = firstIndex;
76
                bestArray = Arrays.copyOf(DNA, DNA.length);
77
                bestCountOnes = countOnes;
78
79
            }
80
            if (countSequence == bestSequence && firstIndex == bestIndex && countOnes > bestCountOnes) {
81
                bestCountRows = countRows;
82
                bestIndex = firstIndex;
83
                bestArray = Arrays.copyOf(DNA, DNA.length);
84
                bestCountOnes = countOnes;
85
86
            }
87
        }
88
        System.out.println(String.format("Best DNA sample %d with sum: %d.", bestCountRows, bestCountOnes));
89
        System.out.println(String.join(" ", bestArray));
90
91
92
    }
93
}