ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€/JAVA] 18311๋ฒˆ : ํฐ ์ˆ˜ ๊ตฌ์„ฑํ•˜๊ธฐ
    Algorithm 2024. 11. 10. 20:55

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

    ๐Ÿ“Œ ๋ฌธ์ œ ์š”์•ฝ 

     

    • ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ, ์ง‘ํ•ฉ KK์˜ ์›์†Œ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
    • KK์˜ ์›์†Œ๋Š” 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ž์—ฐ์ˆ˜๋กœ ์ œํ•œ๋œ๋‹ค.
    • ํ•ญ์ƒ NN๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

     

     



    [ ์˜ค๋‹ต ๋…ธํŠธ ]
    โŒ ๊ธฐ์กด ์˜ค๋‹ต ์ฝ”๋“œ

    import java.io.*;
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
            int N = Integer.parseInt(st.nextToken()); // 657
            int K = Integer.parseInt(st.nextToken()); // 3
            StringTokenizer st2 = new StringTokenizer(br.readLine());
            int answer = 0;
    
            for(int i = N; i >= 1; i--) {
                // i ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด ๋ณ€ํ™˜ ํ›„ ๊ฐ ์ž๋ฆฟ์ˆซ์ž๊ฐ€ 1,5,7 ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์œผ๋จ„
                for(int j = 0; j < K; j++) {
                    if(String.valueOf(i).charAt(j) != st2.nextToken().charAt(0)){
                        break;
                    }else {
                        answer = i;
                        break;
                    }
                }
    
            }
    
            System.out.println(answer);
        }
    }



    ๐Ÿ“Œ ๊ธฐ์กด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

    1. K์˜ ์›์†Œ๋ฅผ ์ œ๋Œ€๋กœ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•จ

     

    • st2.nextToken()์œผ๋กœ K์˜ ์›์†Œ๋ฅผ ๋งค๋ฒˆ ํ•˜๋‚˜์”ฉ ๋ถˆ๋Ÿฌ์˜ค์ง€๋งŒ, ์ด ๋ฐฉ์‹์€ K์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์— ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค.
    • K์˜ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•ด ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

     

     

    2. ๋น„ํšจ์œจ์ ์ธ ํƒ์ƒ‰ ๋ฐฉ์‹

     

    • ๋ถ€ํ„ฐ 1๊นŒ์ง€ ๋ชจ๋“  ์ž์—ฐ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹์€ ๋น„ํšจ์œจ์ ์ด๋‹ค
    • ์˜ˆ๋ฅผ ๋“ค์–ด, N=10000000์ผ ๋•Œ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง„๋‹ค

     

     

    3. ์ž๋ฆฟ์ˆ˜ ๋น„๊ต์˜ ๋…ผ๋ฆฌ ์˜ค๋ฅ˜

    String.valueOf(i).charAt(j)๋กœ i์˜ ์ž๋ฆฟ์ˆ˜์™€ K์˜ ์›์†Œ๋ฅผ ๋น„๊ตํ•˜๋ ค ํ•˜์ง€๋งŒ, ์ด๋Š” ์ž๋ฆฟ์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

     

     



    [ ์ •๋‹ต ์ฝ”๋“œ & ์˜ฌ๋ฐ”๋ฅธ ํ’€์ด ]


    ๐Ÿ“Œ ์˜ฌ๋ฐ”๋ฅธ ํ’€์ด

     

    1. K์˜ ์›์†Œ๋ฅผ ์ œ๋Œ€๋กœ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ

    ์˜ ์›์†Œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ , ์ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ํƒ์ƒ‰

    arr = new int[K];
            for(int i = 0; i < K; i++) {
                arr[i] = Integer.parseInt(st2.nextToken());
                // [1,5,7]
            }

     

     

     

    2. ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS) ๋„์ž…

     

    • ์ž์—ฐ์ˆ˜๋ฅผ ์ž๋ฆฟ์ˆ˜๋ณ„๋กœ ํ™•์žฅํ•˜๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ํƒ์ƒ‰.
    • ์กฐ๊ฑด: total์ด N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ๋งŒ ๊ณ„์† ํƒ์ƒ‰
     if(total > N) return; // 657๋ณด๋‹ค ํฌ๋ฉด ๋ฐ˜ํ™˜
    if(answer < total) answer = total; // ๋‹ต ๊ณ„์† ๊ฐฑ์‹ 

     

     

     

    3. ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ํƒ์ƒ‰

    K์˜ ์›์†Œ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ํฐ ์ˆ˜๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐพ์Œ.

     for(int i = K; i >= 1; i--) { // ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ํƒ์ƒ‰
                // ๋‹ค์Œ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋ถ™์ž„
                dfs(total * 10 + arr[i]);

     

     



    ๐Ÿ“Œ ์ •๋‹ต ์ฝ”๋“œ

    import java.io.*;
    import java.util.*;
    
    public class Main {
        public static int N, K, answer;
        public static int[] arr;
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
            N = Integer.parseInt(st.nextToken()); // 657
            K = Integer.parseInt(st.nextToken()); // 3
            StringTokenizer st2 = new StringTokenizer(br.readLine());
    
            arr = new int[K];
            for(int i = 0; i < K; i++) {
                arr[i] = Integer.parseInt(st2.nextToken());
                // [1,5,7]
            }
    
            Arrays.sort(arr); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    
            dfs(0);
    
            System.out.println(answer);
        }
    
        public static void dfs(int total) {
            if(total > N) return; // 657๋ณด๋‹ค ํฌ๋ฉด ๋ฐ˜ํ™˜
            if(answer < total) answer = total; // ๋‹ต ๊ณ„์† ๊ฐฑ์‹ 
    
            for(int i = K-1; i >= 0; i--) { // ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ํƒ์ƒ‰
                // ๋‹ค์Œ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋ถ™์ž„
                dfs(total * 10 + arr[i]);
                // dfs(0)์ผ๋•Œ,
                // 0 * 10 + arr[2] = 7  --> dfs(7)
                // 0 * 10 + arr[1] = 5  --> dfs(5)
                // 0 * 10 + arr[0] = 1  --> dfs(1)
                // dfs(7)์ผ๋•Œ,
                // 7 * 10 + arr[2] = 77 --> dfs(77)
                // 7 * 10 + arr[1] = 75 --> dfs(75)
                // 7 * 10 + arr[0] = 71 --> dfs(71)
                // dfs(5)์ผ๋•Œ,
                // 5 * 10 + arr[2] = 57 --> dfs(57)
                // 5 * 10 + arr[1] = 55 --> dfs(55)
                // 5 * 10 + arr[0] = 51 --> dfs(51)
                // ...
                // dfs(77)์ผ๋•Œ,
                // 77 * 10 + arr[2] = 777 --> ๋ถˆ๊ฐ€๋Šฅ dfs ์ข…๋ฃŒ
            }
    
        }
    }
Designed by Tistory.