header-img
Info :
ํ‚ค์ง€
Developer

์ •๋‹ต ์ฝ”๋“œ

import java.util.*;

public class BOJ_1062 {
    static int max = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        String[] words = new String[n]; //๋‹จ์–ด ์ €์žฅ
        ArrayList<Character> list = new ArrayList<>(); //๋ฌธ์ž ์ €์žฅ

        //๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์šฐ๋Š” ๋ฌธ์ž๋“ค ๋จผ์ € ์ถ”๊ฐ€
        HashSet<Character> learned = new HashSet<>(Arrays.asList('a', 'n', 't', 'c', 'i'));

        for (int i = 0; i < n; i++) {
            words[i] = sc.next();
            for (char ch : words[i].toCharArray()) {
                if (!learned.contains(ch)) { //๊ธฐ๋ณธ๋ฌธ์ž ์ œ์™ธ
                    if (!list.contains(ch)) { //์ค‘๋ณต๋˜์ง€ ์•„๋‹ˆํ•˜๋ฉด
                        list.add(ch); //๋‚˜๋จธ์ง€ ๋ฌธ์ž๋ฅผ ์ €์žฅ
                    }
                }
            }
        }

        //k<5๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์ฝ์„ ์ˆ˜ ใ„ดใ„ด
        if (k < 5) {
            System.out.println(0);
            return;
        }

        //๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ๋ฌธ์ž ์ˆ˜(list.size()) <= ๋ฐฐ์šธ์ˆ˜์žˆ๋Š” ๋ฌธ์ž์ˆ˜(k-5)๋ฉด ๋‹ค ์ฝ์„ ์ˆ˜ ์žˆ์Œ
        if (list.size() <= k - 5) {
            System.out.println(n);
            return;
        }

        // ์กฐํ•ฉ ์ƒ์„ฑ ๋ฐ ์ตœ๋Œ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด ๊ณ„์‚ฐ
        combine(list, learned, words, 0, 0, k - 5);
        System.out.println(max);
    }

    static void combine(ArrayList<Character> list, HashSet<Character> learned, String[] words, int index, int depth, int maxDepth) {
        //depth๋Š” ์ƒˆ๋กœ ๋ฐฐ์šด ๋ฌธ์ž ์ˆ˜, maxDepth๋Š” ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌธ์ž ์ˆ˜

        //๋‘ ์ˆ˜๊ฐ€ ๊ฐ™์•„์ง€๋ฉด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด ๊ฐœ์ˆ˜ ๊ตฌํ•จ 
        if (depth == maxDepth) {
            max = Math.max(max, countWords(words, learned));
            return;
        }

        for (int i = index; i < list.size(); i++) {
            learned.add(list.get(i)); //์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๋ฐฐ์šฐ๊ธฐ
            combine(list, learned, words, i + 1, depth + 1, maxDepth);
            learned.remove(list.get(i));
        }
    }

    static int countWords(String[] words, HashSet<Character> learned) {
        int count = 0;
        for (String word : words) {
            boolean readable = true;
            for (char ch : word.toCharArray()) {
                if (!learned.contains(ch)) { //๋ฐฐ์šฐ์ง€ ์•Š์€ ๋ฌธ์ž๋ฉด
                    readable = false; //๋ฒ„๋ฆฐ๋‹ค...
                    break;
                }
            }
            if (readable) count++;
        }
        return count;
    }
}

 

 

โฐ time

40๋ถ„

๐Ÿ“Œ Algorithm

๋ธŒ๋ฃจํŠธํฌ์Šค

๐Ÿ“ Logic

์ผ๋‹จ a, n, t, c, i ์ด ๋‹ค์„ฏ ๊ฐœ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์šด๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋‹จ์–ด ์ˆ˜ = k-5๊ฒ ์ง€...

list์— '๋ฐฐ์›Œ์•ผ ํ•  ๋ฌธ์ž'๋ฅผ ์ €์žฅํ•œ๋‹ค.
๊ทธ ๋ฌธ์ž๋“ค์„ ์ตœ๋Œ€ํ•œ ๋ฐฐ์šฐ๊ณ  ๋‚˜์„œ learned์— ์ถ”๊ฐ€, ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค...๋

        //๋‘ ์ˆ˜๊ฐ€ ๊ฐ™์•„์ง€๋ฉด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด ๊ฐœ์ˆ˜ ๊ตฌํ•จ 
        if (depth == maxDepth) {
            max = Math.max(max, countWords(words, learned));
            return;
        }

        for (int i = index; i < list.size(); i++) {
            learned.add(list.get(i)); //์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๋ฐฐ์šฐ๊ธฐ
            combine(list, learned, words, i + 1, depth + 1, maxDepth);
            learned.remove(list.get(i));
        }
 
        for (String word : words) {
            boolean readable = true;
            for (char ch : word.toCharArray()) {
                if (!learned.contains(ch)) { //๋ฐฐ์šฐ์ง€ ์•Š์€ ๋ฌธ์ž๋ฉด
                    readable = false; //๋ฒ„๋ฆฐ๋‹ค...
                    break;
                }
            }
            if (readable) count++;
        }
 

โœ’๏ธ Review

์ด๊ฒƒ๋„ ๋งˆ์Œ๋งŒ ๋จน์œผ๋ฉด ๋‚ ๋กœ ๋จน๋Š” ๊ณจ๋“œ4!

๐Ÿ“กLink

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

๋”๋ณด๊ธฐ
Algorithm (JAVA)/๋ฐฑ์ค€