๊ธฐ๋ก๋ฐฉ

BOJ_1213 : ํŒฐ๋ฆฐ๋“œ๋กฌ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1213 : ํŒฐ๋ฆฐ๋“œ๋กฌ ๋งŒ๋“ค๊ธฐ

Soom_1n 2023. 1. 10. 00:07

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1213๋ฒˆ: ํŒฐ๋ฆฐ๋“œ๋กฌ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ถˆ๊ฐ€๋Šฅํ•  ๋•Œ๋Š” "I'm Sorry Hansoo"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ •๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„œ๋Š” ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ํŒฐ๋ฆฐ๋“œ๋กฌ์œผ๋กœ ๋ฐ”๊พผ๋‹ค. ํŒฐ๋ฆฐ๋“œ๋กฌ์€ ๋ฐ์นผ์ฝ”๋งˆ๋‹ˆ ๋ฌธ์ž์—ด์ด๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ์ข…๋ฅ˜ ์ค‘ ํ™€์ˆ˜ ๊ฐœ๊ฐ€ 1๊ฐœ๋งŒ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String hansu = sc.next();
        int[] alpha = new int[26];

        for (int i = 0; i < hansu.length(); i++) {
            alpha[hansu.charAt(i) - 'A']++;
        }

        int hol_idx = -1;

        for (int i = 0; i < 26; i++) {
            if (alpha[i] % 2 != 0) {
                if (hol_idx == -1)
                    hol_idx = i;
                else {
                    System.out.println("I'm Sorry Hansoo");
                    System.exit(0);
                }
            }
        }
        
        String answer = "";

        for (int i = 0; i < 26; i++) {
            if ((i != hol_idx && alpha[i] > 0) || (i == hol_idx && alpha[i] > 2)) {
                for (int j = 0; j < alpha[i]/2; j++) {
                    answer += (char)('A' + i);
                }
                alpha[i] -= alpha[i]/2;
            }
        }

        if (hol_idx != -1) {
            alpha[hol_idx]--;
            answer += (char) ('A' + hol_idx);
        }

        for (int i = 25; i >= 0; i--) {
            if (alpha[i] > 0) {
                for (int j = 0; j < alpha[i]; j++) {
                    answer += (char)('A' + i);
                }
            }
        }

        System.out.println(answer);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋“ค์„ A~Z๋ฅผ 0~25๋กœ ๋ฐ”๊ฟ” alpha๋ฐฐ์—ด์— ์นด์šดํŠธํ•œ๋‹ค.
  • alpha์—์„œ ํ™€์ˆ˜๊ฐœ ์ธ๋ฑ์Šค๋ฅผ hol_idx์— ์ €์žฅํ•œ๋‹ค.
    • ํ™€์ˆ˜๊ฐœ ์ธ๋ฑ์Šค๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ฉด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์‚ฌ๊ณผ๋ฌธ์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ ์ •๋‹ต์„ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ถœ๋ ฅํ•œ๋‹ค.
    • ํŒฐ๋ฆฐ๋“œ๋กฌ์˜ ์•ž ๋ถ€๋ถ„์„ ๋งŒ๋“ ๋‹ค.
      • hol_idx๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ alpha์— ๊ฐ’์ด ๋‚จ์•˜๊ฑฐ๋‚˜, hol_idx๋ฉด์„œ 2์ด์ƒ์ด๋ฉด ๊ทธ ์ ˆ๋ฐ˜๋งŒํผ ์ •๋‹ต์— ์ถ”๊ฐ€ํ•œ๋‹ค.
      • alpha๊ฐ’์€ 2๋กœ๋‚˜๋ˆˆ ๋ชซ์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
    • ํŒฐ๋ฆฐ๋“œ๋กฌ์˜ ์ค‘๊ฐ„ ๋ถ€๋ถ„์„ ๋งŒ๋“ ๋‹ค.
      • ํ™€์ˆ˜๊ฐœ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ์—ˆ๋‹ค๋ฉด, ์ •๋‹ต์— ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ํŒฐ๋ฆฐ๋“œ๋กฌ์˜ ๋’ท ๋ถ€๋ถ„์„ ๋งŒ๋“ ๋‹ค.
      • ๋‚จ์€ alpha์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋‹ต์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ์ •๋‹ต ๋ฌธ์ž์—ด answer์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋‹ค ํ’€๊ณ ๋ณด๋‹ˆ ๊ฐœ๋…์€ ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€ ์•Š์•˜๋Š”๋ฐ, ํŒฐ๋ฆฐ๋“œ๋กฌ์„ ์™„์„ฑํ•  ๋•Œ ์•ž ๋ถ€๋ถ„๊ณผ ๋’ท ๋ถ€๋ถ„์˜ ์กฐ๊ฑด์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅธ๊ฒŒ ํž˜๋“ค๊ฒŒํ–ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_2407 : ์กฐํ•ฉ  (0) 2023.01.10
BOJ_10974 : ๋ชจ๋“  ์ˆœ์—ด  (0) 2023.01.10
BOJ_1183 : ์•ฝ์†  (0) 2023.01.09
BOJ_1166 : ์„ ๋ฌผ  (0) 2023.01.06
BOJ_1072 : ๊ฒŒ์ž„  (0) 2023.01.04