Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

Delyan99 avatar Delyan99 4 Точки

9.*Kamino Factory

9.*Kamino Factory

The clone factory in Kamino got another order to clone troops. But this time you are tasked to find the best DNA sequence to use in the production.

You will receive the DNA length and until you receive the command "Clone them!" you will be receiving a DNA sequences of ones and zeroes, split by "!" (one or several).

You should select the sequence with the longest subsequence of ones. If there are several sequences with same length of subsequence of ones, print the one with the leftmost starting index, if there are several sequences with same length and starting index, select the sequence with the greater sum of its elements.

After you receive the last command "Clone them!" you should print the collected information in the following format:

"Best DNA sample {bestSequenceIndex} with sum: {bestSequenceSum}."

"{DNA sequence, joined by space}"

Input / Constraints

  • The first line holds the length of the sequencesinteger in range [1…100];
  • On the next lines until you receive "Clone them!" you will be receiving sequences (at least one) of ones and zeroes, split by "!" (one or several).

 Output

The output should be printed on the console and consists of two lines in the following format:

"Best DNA sample {bestSequenceIndex} with sum: {bestSequenceSum}."

"{DNA sequence, joined by space}"

 Examples

Input

Output

Comments

5

1!0!1!1!0

0!1!1!0!0

Clone them!

Best DNA sample 2 with sum: 2.

0 1 1 0 0

We receive 2 sequences with same length of subsequence of ones, but the second is printed, because its subsequence starts at index[1].

Input

Output

Comments

4

1!1!0!1

1!0!0!1

1!1!0!0

Clone them!

Best DNA sample 1 with sum: 3.

1 1 0 1

We receive 3 sequences. Both 1 and 3 have same length of subsequence of ones -> 2, and both start from index[0], but the first is printed, because its sum is greater.

 

Здвавейте, задачата ми гърми на Test #4 в judge. Получавам 90/100. Ако някой може да ми помогне бих бил много благодарен.

https://pastebin.com/YQTdCTVR 

Тагове:
0
Java Fundamentals 24/06/2020 00:49:34
MartinBG avatar MartinBG 4803 Точки
Best Answer

Не е покрита следната част от условието на задачата:

if there are several sequences with same length and starting index, select the sequence with the greater sum of its elements.

 

Ето примерен тест, който демонстрира това:

5
0!1!1!0!0
0!1!1!0!1
Clone them!

 

Грешен резултат:

Best DNA sample 1 with sum: 2.
0 1 1 0 0 

 

Верен резултат:

Best DNA sample 2 with sum: 3.
0 1 1 0 1 

 

0
24/06/2020 01:21:21
Delyan99 avatar Delyan99 4 Точки

Благодаря! Направих я 100/100.

1
kneav avatar kneav 7 Точки

Едно решение от мен само с Array

 

import java.util.Arrays;
import java.util.Scanner;

public class KaminoFactory {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());//length of the sequences – integer in range [1…100];
        String code = scanner.nextLine();

        int numberDNAtest = 0;//real = 0+1
        int[] bestLenghtInRow = new int[1000];// All best One`s  lenght
        int[] bestPositionInRow = new int[1000];// All best position in row
        int[] sumRow = new int[1000];// all sum
        String[] DNA = new String[1000]; // All sequences


        while (!"Clone them!".equals(code)) {
            code = code.replaceAll("!+", "");
            DNA[numberDNAtest] = code;

            int[] num = Arrays.stream(code.split("")).mapToInt(e -> Integer.parseInt(e)).toArray();
            int[] len = new int[num.length];

            for (int i = 0; i < num.length; i++) { // line long seq of Ones +sum
                if (num[i] == 1) {
                    len[i] = 1;
                    for (int j = i + 1; j < num.length; j++) {
                        if (num[j] == 1) {
                            len[i] += 1;
                        } else {
                            break;
                        }
                    }
                }
                sumRow[numberDNAtest] += num[i];
            }

            for (int i = 0; i < num.length; i++) { // best lenght in row goes to Array
                if (len[i] > bestLenghtInRow[numberDNAtest]) {
                    bestLenghtInRow[numberDNAtest] = len[i];
                    bestPositionInRow[numberDNAtest] = i;
                }
            }
            numberDNAtest++;
            code = scanner.nextLine();
        }

        int grandLenght = 0;
        int grandposition = 0;
        String grand = DNA[0];
        int grandSum = 0;
        int grandRowNum = 1;

        for (int i = 0; i < numberDNAtest; i++) {
            if (bestLenghtInRow[i] > grandLenght ||
                    (bestLenghtInRow[i] == grandLenght && grandposition > bestPositionInRow[i]) ||
                    (bestLenghtInRow[i] == grandLenght && grandposition == bestPositionInRow[i] && grandSum < sumRow[i])) {
                grandLenght = bestLenghtInRow[i];
                grandposition = bestPositionInRow[i];
                grand = DNA[i];
                grandSum = sumRow[i];
                grandRowNum = i + 1;
            }
        }
        grand = grand.replaceAll("", " ");
        grand = grand.substring(1);
        System.out.printf("Best DNA sample %d with sum: %d.%n%s", grandRowNum, grandSum, grand);

    }
}
 

 

Голямо ивъртане беше, докато не разбрах, че трябва да дели ред различен от последователни !.

Някъде тук срещанх примерни тестове разлини от Нулевите:

 

5
0!0!0!!!!!0!1
1!0!1!1!0
0!1!1!0!0
0!0!0!!!!!0!1
Clone them!

5
00!001
10!101
011!00
000!!!0!1
Clone them!

5
00000
!000!00!
0!0!0!0!0
Clone them!


5
00!001
10!101
01!001
000!!!0!1
Clone them!

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.