inblog logo
|
LHS's Study Space
    알고리즘문제풀기랜덤마라톤

    [랜덤 마라톤] 비슷한 번호판(21980)

    lhs's avatar
    lhs
    Jan 02, 2025
    [랜덤 마라톤] 비슷한 번호판(21980)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    www.acmicpc.net
    https://www.acmicpc.net/problem/21980
    notion image
    notion image

    1. 문제 풀이 아이디어

    • Map을 활용하여 번호판 데이터를 저장하고 이를 통해 문제를 해결할 수 있다.

    2. 나의 정답 코드

    public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder stringBuilder = new StringBuilder(); int t = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < t; i++) { StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); int n = Integer.parseInt(stringTokenizer.nextToken()); int k = Integer.parseInt(stringTokenizer.nextToken()); String[] x = bufferedReader.readLine().split(" "); Map<String, Integer> map = new HashMap<>(); for (int j = 0; j < n; j++) { int count = 0; char[] cs = x[j].toCharArray(); for (int l = 0; l < k; l++) { if (cs[l] < 'a') { count++; cs[l] += 32; } } Arrays.sort(cs); String s = String.valueOf(cs) + count; map.put(s, map.getOrDefault(s, 0) + 1); } int result = 0; for (int j : map.values()) { if (j == 1) continue; result += j * (j - 1) / 2; } stringBuilder.append(result).append('\n'); } System.out.print(stringBuilder); bufferedReader.close(); } }

    3. 정리

    • 입력받은 문자열을 char 배열로 변환한 후, 각 문자를 순회하며 대문자를 소문자로 변환하고 변환된 대문자의 개수를 count로 센다.
    • 변환된 char 배열을 정렬한 후, 배열과 대문자 개수를 결합하여 문자열로 만든다.
    • 생성된 문자열을 Map에 저장하여 비슷한 번호판을 그룹화한다.
    • Map의 값을 순회하며 값이 1이 아닌 경우 쌍의 수를 계산하고 결과값에 더해 최종적으로 문제를 해결한다.
    Share article
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리

    LHS's Study Space

    RSS·Powered by Inblog