-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon2661.java
65 lines (49 loc) · 1.53 KB
/
Baekjoon2661.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package algo.Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* https://www.acmicpc.net/problem/2661
* 백준 2661번 좋은 수열
*/
public class Baekjoon2661 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int[] numbers = {1, 2, 3};
static int M;
static boolean flag = false;
static StringBuilder answer = new StringBuilder();
public static void main(String[] args) throws IOException {
M = Integer.parseInt(br.readLine());
answer.append(numbers[0]);
dfs(0);
}
private static void dfs(int i) {
if(isBad() || flag) return;
if (answer.length() == M) {
flag = true;
System.out.println(answer.toString());
return;
}
for (int j = 0; j < 3; j++) {
int next = j;
if(next == i) continue;
answer.append(numbers[next]);
dfs(next);
answer.delete(answer.length() - 1, answer.length());
}
}
private static boolean isBad() {
String str = answer.toString();
if(str.length()==1) return false;
for (int i = str.length()-1; i > 0 ; i--) {
String pivot = str.substring(i);
if(i-pivot.length()<0) continue;
String check = str.substring(i-pivot.length(), i);
if(pivot.equals(check)){
return true;
}
}
return false;
}
}