Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_2661] 좋은 수열 본문

Study/Problem Solving

[BOJ_2661] 좋은 수열

monitor 2023. 10. 15. 22:32

설명


체크하는 로직이 중점이고 나머지는 일반 순열 만들듯이 진행하면 된다.

코드


package backtracking;
import java.io.*;
import java.util.*;
public class BOJ_2661 {
    static int N;
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine()," ");

        N = Integer.parseInt(tk.nextToken());
        go(0,"");
    }

    public static void go(int idx,String str){
        if(idx == N){
            if(check(str)){
                System.out.println(str);
                System.exit(0);
            }
            return;
        }
        for(int i = 1; i <= 3; i++){
            if(check(str  + (i + ""))) {
                go(idx + 1, str + (i + ""));
            }
        }
    }
    public static boolean check(String str){
        for(int i = 1; i <= str.length()/2; i++) {
            String front = str.substring(str.length()-i*2, str.length()-i);
            String back = str.substring(str.length()-i);

            if(front.equals(back)) 
            	return false;
        }

        return true;
    }
}
 

2661번: 좋은수열

첫 번째 줄에 1, 2, 3으로만 이루어져 있는 길이가 N인 좋은 수열들 중에서 가장 작은 수를 나타내는 수열만 출력한다. 수열을 이루는 1, 2, 3들 사이에는 빈칸을 두지 않는다.

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_17136] 색종이 붙이기  (0) 2023.10.17
[BOJ_22944] 죽음의 비  (1) 2023.10.16
[BOJ_18430] 무기 공학  (0) 2023.10.15
[BOJ_6443] 애너그램  (0) 2023.10.14
[BOJ_3980] 선발 명단  (0) 2023.10.12