카테고리 없음

[해커랭크] ACM ICPC Team

snapcoder 2025. 12. 12. 00:49
728x90
반응형
SMALL

[해커랭크] ACM ICPC Team

 

https://www.hackerrank.com/challenges/acm-icpc-team/problem?isFullScreen=true

 

ACM ICPC Team | HackerRank

Print the maximum topics a given team can cover for ACM ICPC World Finals

www.hackerrank.com

 

 

There are a number of people who will be attending ACM-ICPC World Finals. Each of them may be well versed in a number of topics. Given a list of topics known by each attendee, presented as binary strings, determine the maximum number of topics a 2-person team can know. Each subject has a column in the binary string, and a '1' means the subject is known while '0' means it is not. Also determine the number of teams that know the maximum number of topics. Return an integer array with two elements. The first is the maximum number of topics known, and the second is the number of teams that know that number of topics.ACM-ICPC 월드 파이널에 참석할 인물들이 여러 명 있습니다. 각각은 여러 주제에 대해 잘 알고 있을 수 있습니다. 각 참가자가 알고 있는 주제 목록을 이진 문자열로 제시하면, 2인 팀이 알 수 있는 최대 주제 수를 결정합니다. 각 주전자는 이진 문자열에 열이 있으며, '1'은 해당 주어가 알려져 있음을 의미하고 '0'은 알려지지 않았다는 뜻입니다. 또한 최대 주제 수를 아는 팀 수를 정하세요. 두 개의 요소로 이루어진 정수 배열을 반환합니다. 첫 번째는 알려진 최대 주제 수이고, 두 번째는 그 주제 수를 아는 팀 수입니다.

Example본보기


The attendee data is aligned for clarity below:참석자 데이터는 명확성을 위해 아래에 정렬되어 있습니다:

10101
11110
00010

These are all possible teams that can be formed:이들은 모두 구성할 수 있는 가능한 팀들입니다:

Members Subjects
(1,2)   [1,2,3,4,5]
(1,3)   [1,3,4,5]
(2,3)   [1,2,3,4]

In this case, the first team will know all 5 subjects. They are the only team that can be created that knows that many subjects, so  is returned.이 경우 첫 번째 팀은 5개 과목 모두를 알게 됩니다. 그들은 그렇게 많은 주제를 아는 유일한 팀이기 때문에 다시 돌아옵니다.

Function Description기능 설명

Complete the acmTeam function in the editor below.
acmTeam has the following parameter(s):아래 편집기에서 acmTeam 기능을 완료하세요. acmTeam은 다음과 같은 매개변수를 가집니다:

  • string topic: a string of binary digits문자열 주제: 이진 숫자 문자열

Returns반환

  • int[2]: the maximum topics and the number of teams that know that many topics정수[2]: 최대 주제와 그만큼 많은 주제를 아는 팀 수

Input Format입력 형식

The first line contains two space-separated integers  and , where  is the number of attendees and  is the number of topics.첫 번째 줄은 두 개의 공간 분리 정수와 를 포함하며, 여기서 는 참석자 수이고 는 주제 수이다.

Each of the next  lines contains a binary string of length .다음 각 줄은 길이 의 이진 문자열을 포함한다.

Constraints제약 조건


Sample Input샘플 입력

4 5
10101
11100
11010
00101

Sample Output샘플 출력

5
2

Explanation설명

Calculating topics known for all permutations of 2 attendees we get:2명의 참석자 모든 조합에 대해 알려진 주제를 계산하면:






The 2 teams (1, 3) and (3, 4) know all 5 topics which is maximal.두 팀(1, 3)과 (3, 4)는 5개 주제 모두를 알고 있어 최대 수준입니다.

 

 

 

 

 

 

 

 

풀이 : or조건으로 개수세서 최대수준 카운팅

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'acmTeam' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts STRING_ARRAY topic as parameter.
     */

    public static List<Integer> acmTeam(List<String> topic) {
    // Write your code here
   
        int n = topic.size();
        int m = topic.get(0).length();
        int sameCharCount = 0;
        int maxCount = 0;
       
        for(int i=0; i<n-1; i++){
            String a = topic.get(i);
            for(int j=i+1; j<n; j++){
                // compare i, j
                String b = topic.get(j);
                int orCount = 0;
                for(int k=0; k<m; k++){
                    if (a.charAt(k)=='1' || b.charAt(k)=='1'){
                        orCount++;
                    }
                }
                if(sameCharCount < orCount){
                    sameCharCount = orCount;
                    maxCount = 1;
                }
                else if(sameCharCount == orCount){
                    maxCount++;
                }
            }
        }
       
        List<Integer> dap = new ArrayList<>();
        dap.add(sameCharCount);
        dap.add(maxCount);
        return dap;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(firstMultipleInput[0]);

        int m = Integer.parseInt(firstMultipleInput[1]);

        List<String> topic = IntStream.range(0, n).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        List<Integer> result = Result.acmTeam(topic);

        bufferedWriter.write(
            result.stream()
                .map(Object::toString)
                .collect(joining("\n"))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

 

 

728x90
반응형
LIST