๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][๋ฐฑ์ค€] 1062๋ฒˆ ๊ฐ€๋ฅด์นจ

๋ฐ˜์‘ํ˜•

[BOJ] 1062๋ฒˆ ๊ฐ€๋ฅด์นจ

https://www.acmicpc.net/problem/1062

๋ฌธ์ œ ์ ‘๊ทผ

๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ด๋ณด๋Š” ๋ฌธ์ œ์˜€๋‹ค.
์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ํฌํ•จํ•˜๋Š” anta, tica ์— ํฌํ•จ๋œ 5๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ํ•„์ˆ˜ ๋‹จ์–ด๋กœ ํฌํ•จ์‹œํ‚ค๊ณ , ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์„œ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ์ค„์˜€๊ณ , ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ 5๊ฐœ ๋ณด๋‹ค ์ ์„๋•Œ 0, 26๊ฐœ ์ผ ๋•Œ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ n์„ ์ถœ๋ ฅํ–ˆ๋‹ค.
๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ•  ๋•Œ๋„ a~z๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•™์Šตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•ด์„œ ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ๊ณณ์€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค.

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ1062 {
    static boolean[] alphabet;
    static String[] words;
    static int answer = Integer.MIN_VALUE;
    static int k,n;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        k = Integer.parseInt(s[1]);
        words = new String[n];
        for(int i = 0; i<n; i++){
            String str = br.readLine();
            //๋ชจ๋“  ๋‹จ์–ด๊ฐ€ anta์™€ tica๋ฅผ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ฒ˜๋ฆฌ
            //ํ•„์ˆ˜๋กœ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ a, c, i, n, t ๊ฐ€ ์žˆ์Œ
            str = str.replace("anta", "");
            str = str.replace("tica", "");
            words[i] = str;
        }
        if(k<5) {
            // 5๊ฐœ๋ณด๋‹ค ์ ์œผ๋ฉด a, c, i, n, t ๋ฅผ ๋ฐฐ์šฐ์ง€ ๋ชปํ•จ
            System.out.println(0);
            return;
        } else if(k==26) {
            // ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ๋‹ค ๋ฐฐ์šฐ๋Š” ๊ฒฝ์šฐ
            System.out.println(n);
            return;
        }
        alphabet = new boolean[26];
        alphabet[0] = true; //a
        alphabet[2] = true; //c
        alphabet[8] = true; // i
        alphabet[13] = true; //n
        alphabet[19] = true; //t
        studyWord(1,5);
        System.out.println(answer);
    }


    public static void studyWord(int start, int alphabetCnt){
        // ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ํƒ์ƒ‰.
        // 0~26์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— start ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋ฏธ ์ง€๋‚˜์˜จ ๊ณณ์€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ฒŒ ํ•ด์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ž„.
        if(alphabetCnt == k){
            int cnt = 0;
            for(int i = 0; i<n; i++){
                boolean flag = true;
                for(int j = 0; j< words[i].length(); j++){
                    // words[i]์— ํฌํ•จ๋œ ์•ŒํŒŒ๋ฒณ ์ค‘์— ์•ˆ๋ฐฐ์šด ์•ŒํŒŒ๋ฒณ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
                    if(!alphabet[words[i].charAt(j) - 'a']){
                        flag = false;
                        break;
                    }
                }
                if(flag) cnt++;
            }
            answer = Math.max(answer, cnt);
            return;
        }
        for(int i = start; i<26; i++){
            if(!alphabet[i]){
                alphabet[i] = true;
                studyWord(i,alphabetCnt+1);
                alphabet[i] = false;
            }
        }

    }
}
๋ฐ˜์‘ํ˜•