본문 바로가기
Problem Solving/Baekjoon

[백준] 9742 순열 - Backtracking / Java

by graycode 2023. 2. 25.

 문제 링크

 

9742번: 순열

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 문자열은 서로 다른 숫자와 알파벳으로 이루어져 있으며, 길이는 최대 10이다. 또한, 사전

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {

    static String perm, res;
    static int idx, cnt;
    static char[] arr;
    static boolean[] visit;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        String input;
        while ((input = br.readLine()) != null) {
            st = new StringTokenizer(input);
            perm = st.nextToken();
            idx = Integer.parseInt(st.nextToken());

            cnt = 0;
            arr = new char[perm.length()];
            visit = new boolean[perm.length()];

            recur(0);

            res = cnt < idx ? "No permutation" : res;
            bw.write(perm + " " + idx + " = " + res + "\n");
        }

        bw.flush();
    }

    private static void recur(int depth) {
        if (depth == perm.length()) {
            cnt++;
            if (cnt == idx)
                res = String.valueOf(arr);

            return;
        }

        for (int i = 0; i < perm.length(); i++) {
            if (visit[i])
                continue;

            visit[i] = true;
            arr[depth] = perm.charAt(i);
            recur(depth + 1);
            visit[i] = false;
        }
    }

}

댓글