From d46e888907f71f919e2f8dba6de54e31ef2cc8ea Mon Sep 17 00:00:00 2001 From: ComelyU Date: Sun, 8 Oct 2023 13:49:47 +0900 Subject: [PATCH 01/10] [solved] boj2792 --- HuhJunHyeok/boj/boj2792.java | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 HuhJunHyeok/boj/boj2792.java diff --git a/HuhJunHyeok/boj/boj2792.java b/HuhJunHyeok/boj/boj2792.java new file mode 100644 index 00000000..35a9668f --- /dev/null +++ b/HuhJunHyeok/boj/boj2792.java @@ -0,0 +1,68 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +/** + * [boj] 2792. 보석 상자 + */ +public class boj2792 { + /** + * N: 아이들의 수, M: 색상의 수, maxCount: 같은 색 보석의 최대 수, minEnvy: 질투심의 최솟값 + */ + static int N, M, maxCount = 0, minEnvy = Integer.MAX_VALUE; + /** + * jewelryCntByColor: 보석 색상에 따른 개수 + */ + static int[] jewelryCntByColor; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + jewelryCntByColor = new int[M]; + for(int i = 0 ; i < M; i++) { + jewelryCntByColor[i] = Integer.parseInt(br.readLine()); + if(maxCount < jewelryCntByColor[i]) { + maxCount = jewelryCntByColor[i]; + } + } + + parametricSearch(); + + System.out.println(minEnvy); + } + + public static void parametricSearch() { + int low = 1; + int high = maxCount; + + while(low <= high) { + int mid = (low + high) / 2; + + if(divPeople(mid) <= N) { + high = mid - 1; + minEnvy = mid; + } else { + low = mid + 1; + } + } + } + + /** + * parameter로 들어온 질투심(= 가장 많은 보석을 가져간 학생이 가진 보석의 수)을 가정. 이때 몇 명이 나눠 가지나. + * @param envy : 질투심 + * @return + */ + public static int divPeople(int envy) { + int divPeople = 0; + for(int jewelryCnt: jewelryCntByColor) { + // 모든 보석을 다 나누어 주어야 한다. + divPeople += jewelryCnt % envy == 0 ? jewelryCnt / envy : jewelryCnt / envy + 1; + } + + return divPeople; + } +} From 86b553d5cc0f97040acd76fa53bdc7d7a0530143 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Sun, 8 Oct 2023 20:40:26 +0900 Subject: [PATCH 02/10] [solved] boj16234 --- HuhJunHyeok/boj/boj16234.java | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 HuhJunHyeok/boj/boj16234.java diff --git a/HuhJunHyeok/boj/boj16234.java b/HuhJunHyeok/boj/boj16234.java new file mode 100644 index 00000000..4eb0504e --- /dev/null +++ b/HuhJunHyeok/boj/boj16234.java @@ -0,0 +1,128 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Queue; +import java.util.StringTokenizer; + +/** + * [boj] 16234. 인구 이동 + */ +public class boj16234 { + /** + * Country: 나라를 의미하는 class + */ + static class Country { + int r, c; + + public Country(int r, int c) { + this.r = r; + this.c = c; + } + } + /** + * N: 땅의 한 라인의 크기, L: 국경선 여는 인구 차이 하한값, R: 국경선 여는 인구 차이 상한값 + * days: 인구 이동이 며칠 동안 발생하는지, openBoarderCountryCnt: 국경이 열린 나라의 인구 수 합 + */ + static int N, L, R, days, openBoarderCountryPopulation; + /** + * map: 땅을 나타내는 지도 + */ + static int[][] map; + static boolean possibleToMove; + static boolean[][] visited; + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; + static ArrayList countryList = new ArrayList<>(); + static Queue countryQueue = new ArrayDeque<>(); + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + + N = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + R = Integer.parseInt(st.nextToken()); + + map = new int[N][N]; + for(int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine(), " "); + for(int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + } + } + + movePopulation(); + + System.out.println(days); + } + + /** + * bfs를 통해 인구 이동이 가능한지 파악하여 인구 이동이 불가능 할 때까지 인구 이동 진행. + */ + public static void movePopulation() { + while(true) { // 인구 이동이 불가능할 때까지 반복 + possibleToMove = false; + visited = new boolean[N][N]; + + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + if(!visited[i][j]) { +// countryQueue = new ArrayDeque<>(); +// countryList = new ArrayList<>(); + countryList.clear(); + openBoarderCountryPopulation = 0; + bfs(i, j); + + if(countryList.size() > 1) { // 국경이 열린 나라가 두 나라 이상인 경우 인구 이동이 일어남. + possibleToMove = true; + + int avgPopulation = openBoarderCountryPopulation / countryList.size(); + for(Country country: countryList) { + map[country.r][country.c] = avgPopulation; + } + } + } + } + } + + if(!possibleToMove) { + return; + } + days++; + } + } + + public static void bfs(int r, int c) { + countryQueue.offer(new Country(r, c)); + countryList.add(new Country(r, c)); + + visited[r][c] = true; + openBoarderCountryPopulation = map[r][c]; + + while(!countryQueue.isEmpty()) { + Country nowCountry = countryQueue.poll(); + + for(int i = 0; i < 4; i ++) { + int nextR = nowCountry.r + dr[i]; + int nextC = nowCountry.c + dc[i]; + + if(!isInMap(nextR, nextC) || visited[nextR][nextC]) { + continue; + } + + int diff = Math.abs(map[nowCountry.r][nowCountry.c] - map[nextR][nextC]); + if(L <= diff && diff <= R) { + countryQueue.offer(new Country(nextR, nextC)); + countryList.add(new Country(nextR, nextC)); + + visited[nextR][nextC] = true; + openBoarderCountryPopulation += map[nextR][nextC]; + } + } + } + } + + public static boolean isInMap(int r, int c) { + return 0 <= r && r < N && 0 <= c && c < N; + } +} From 8f60edb5cb8f96e47337300ff9f6f8344e0c7d69 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Mon, 9 Oct 2023 14:47:31 +0900 Subject: [PATCH 03/10] [solved] boj16719 --- HuhJunHyeok/boj/boj16719.java | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 HuhJunHyeok/boj/boj16719.java diff --git a/HuhJunHyeok/boj/boj16719.java b/HuhJunHyeok/boj/boj16719.java new file mode 100644 index 00000000..018bc49f --- /dev/null +++ b/HuhJunHyeok/boj/boj16719.java @@ -0,0 +1,58 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * [boj] 16719. ZOAC + */ +public class boj16719 { + /** + * text: 주어진 문자열 + */ + static String text; + /** + * textLength: 문자열의 길이 + */ + static int textLength; + /** + * check: 문자열에서 각 index에 해당하는 문자(글자)를 사용 여부 체크. + */ + static boolean[] check; + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + text = br.readLine(); + textLength = text.length(); + check = new boolean[textLength]; + + recur(0, textLength - 1); + + System.out.print(sb.toString()); + } + + public static void recur(int leftIdx, int rightIdx) { + if(leftIdx > rightIdx) { + return; + } + + int idx = leftIdx; + for(int i = leftIdx; i <= rightIdx; i++) { // leftIdx ~ rightIdx에서 사전순 가장 빠른 글자 찾기 + if(text.charAt(i) < text.charAt(idx)) { + idx = i; + } + } + check[idx] = true; + + for(int i = 0; i < textLength; i++) { + if(check[i]) { + sb.append(text.charAt(i)); + } + } + sb.append("\n"); + + // 사전순 가장 빠른 글자를 기준으로 오른쪽, 왼쪽 순서로 확인. + recur(idx + 1, rightIdx); + recur(leftIdx, idx - 1); + } +} From b249806638ba7b9889d03aed58d74623b1a070f1 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Wed, 11 Oct 2023 23:47:34 +0900 Subject: [PATCH 04/10] [solved] boj1701 --- HuhJunHyeok/boj/boj1701.java | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 HuhJunHyeok/boj/boj1701.java diff --git a/HuhJunHyeok/boj/boj1701.java b/HuhJunHyeok/boj/boj1701.java new file mode 100644 index 00000000..dbac0f16 --- /dev/null +++ b/HuhJunHyeok/boj/boj1701.java @@ -0,0 +1,55 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * [boj] 1701. Cubeditor + */ +public class boj1701 { + /** + * text: 전체 문자열 + * subText: 패턴 문자열 + */ + static String text, subText; + /** + * maxLength: 두 번 이상 나오는 부분 문자열의 최장 길이 + */ + static int maxLength; + /** + * pi: 부분 일치 테이블 + */ + static int[] pi; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + text = br.readLine(); + + // KMP 알고리즘 이용 + for(int i = 0; i < text.length(); i++) { + subText = text.substring(i); + + getPi(); + } + + System.out.println(maxLength); + } + + /** + * 부분 일치 테이블 구하기 + */ + public static void getPi() { + pi = new int[subText.length()]; + + // j: 접두사 index, i: 접미사 index + int j = 0; + for(int i = 1; i < subText.length(); i++) { + while(j > 0 && subText.charAt(i) != subText.charAt(j)) { + j = pi[j - 1]; + } + + if(subText.charAt(i) == subText.charAt(j)) { + maxLength = Math.max(maxLength, pi[i] = ++j); + } + } + } +} From eba36a912c64b458a4017cb6d952e8dec8583df4 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Mon, 16 Oct 2023 19:17:11 +0900 Subject: [PATCH 05/10] [solved] boj4811 --- HuhJunHyeok/boj/boj4811.java | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 HuhJunHyeok/boj/boj4811.java diff --git a/HuhJunHyeok/boj/boj4811.java b/HuhJunHyeok/boj/boj4811.java new file mode 100644 index 00000000..6b8ab512 --- /dev/null +++ b/HuhJunHyeok/boj/boj4811.java @@ -0,0 +1,45 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * [boj] 4811. 알약 + */ +public class boj4811 { + /** + * N: 병에 들어있는 약의 개수 + */ + static int N; + /** + * dp[x][y]: 알약 한 조각이 x개, 반 조각이 y개 있는 상태에서의 가능한 서로 다른 문자열의 수. + * 최종 답은 dp[N][0]이다. + * 알약 반 조각이 없는 상황. 즉, y == 0인 상황이면 + * dp[x][y] = dp[x - 1][y + 1] + * 알약 반 조각이 있는 상황. 즉, y > 0인 상황이면 + * dp[x][y] = dp[x - 1][y + 1] + dp[x][y - 1] + */ + static long[][] dp = new long[31][31]; + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + for(int i = 1; i <= 30; i++) { + dp[0][i] = 1; // 한 조각이 없고 반 조각만 있으면 HHH... 의 한 가지 경우. + } + for(int i = 1; i <= 30; i++) { + for(int j = 0; j < 30; j++) { + if(j == 0) { + dp[i][j] = dp[i - 1][j + 1]; + } else { + dp[i][j] = dp[i - 1][j + 1] + dp[i][j - 1]; + } + } + } + + while((N = Integer.parseInt(br.readLine())) != 0) { + sb.append(dp[N][0]).append("\n"); + } + + System.out.print(sb.toString()); + } +} From 66079a240d237e686ee7a34b08a70922a9c2898a Mon Sep 17 00:00:00 2001 From: ComelyU Date: Mon, 16 Oct 2023 19:17:43 +0900 Subject: [PATCH 06/10] [solved] boj16197 --- HuhJunHyeok/boj/boj16197.java | 126 ++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 HuhJunHyeok/boj/boj16197.java diff --git a/HuhJunHyeok/boj/boj16197.java b/HuhJunHyeok/boj/boj16197.java new file mode 100644 index 00000000..6f1bbe90 --- /dev/null +++ b/HuhJunHyeok/boj/boj16197.java @@ -0,0 +1,126 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +/** + * [boj] 16197. 두 동전 + */ +public class boj16197 { + static class Coin { + int r; + int c; + + public Coin(int r, int c) { + this.r = r; + this.c = c; + } + } + static class CoinsPositionCount { + int r1; + int c1; + int r2; + int c2; + int pressCount; + + public CoinsPositionCount(int r1, int c1, int r2, int c2, int pressCount) { + this.r1 = r1; + this.c1 = c1; + this.r2 = r2; + this.c2 = c2; + this.pressCount = pressCount; + } + } + /** + * N: 보드의 세로 크기, M: 보드의 가로 크기 + */ + static int N, M, minPressCount = -1; + /** + * map: N*M 크기의 보드 + */ + static char[][] map; + /** + * coins: 코인 + */ + static Coin[] coins = new Coin[2]; + static boolean[][][][] visited; + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new char[N][M]; + int coinIdx = 0; + for(int i = 0; i < N; i++) { + String line = br.readLine(); + for(int j = 0; j < M; j++) { + map[i][j] = line.charAt(j); + + if(map[i][j] == 'o') { + coins[coinIdx++] = new Coin(i, j); + } + } + } + + bfs(); + + System.out.println(minPressCount); + } + + public static void bfs() { + Queue queue = new ArrayDeque<>(); + queue.offer(new CoinsPositionCount(coins[0].r, coins[0].c, coins[1].r, coins[1].c, 0)); + + visited = new boolean[N][M][N][M]; + visited[coins[0].r][coins[0].c][coins[1].r][coins[1].c] = true; + + while(!queue.isEmpty()) { + CoinsPositionCount now = queue.poll(); + + if(now.pressCount >= 10) { + return; + } + + for(int i = 0; i < 4; i++) { + int nextR1 = now.r1 + dr[i]; + int nextC1 = now.c1 + dc[i]; + int nextR2 = now.r2 + dr[i]; + int nextC2 = now.c2 + dc[i]; + + int dropCount = 0; // 떨어진 않은 동전의 수 + if(isInMap(nextR1, nextC1) && map[nextR1][nextC1] == '#') { + nextR1 = now.r1; + nextC1 = now.c1; + } + if(!isInMap(nextR1, nextC1)) { + dropCount++; + } + if(isInMap(nextR2, nextC2) && map[nextR2][nextC2] == '#') { + nextR2 = now.r2; + nextC2 = now.c2; + } + if(!isInMap(nextR2, nextC2)) { + dropCount++; + } + + if(dropCount == 1) { + minPressCount = now.pressCount + 1; + return; + } + if(dropCount == 0 && !visited[nextR1][nextC1][nextR2][nextC2]) { + queue.offer(new CoinsPositionCount(nextR1, nextC1, nextR2, nextC2, now.pressCount + 1)); + visited[nextR1][nextC1][nextR2][nextC2] = true; + } + } + } + } + + public static boolean isInMap(int r, int c) { + return 0 <= r && r < N && 0 <= c && c < M; + } +} From 7342f8364e7e666524c0241b204d506953c47871 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Mon, 16 Oct 2023 19:17:54 +0900 Subject: [PATCH 07/10] [solved] boj17951 --- HuhJunHyeok/boj/boj17951.java | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 HuhJunHyeok/boj/boj17951.java diff --git a/HuhJunHyeok/boj/boj17951.java b/HuhJunHyeok/boj/boj17951.java new file mode 100644 index 00000000..d69bb8c8 --- /dev/null +++ b/HuhJunHyeok/boj/boj17951.java @@ -0,0 +1,68 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +/** + * [boj] 17951. 흩날리는 시험지 속에서 내 평점이 느껴진거야 + */ +public class boj17951 { + /** + * N: 시험지의 수, K: 시험지를 나눌 그룹의 수, totalScore: 시험지의 맞은 수의 합, maxScore: 현수가 받을 수 있는 최대 점수 + */ + static int N, K, totalScore, maxScore; + /** + * testPapers: 시험지 + */ + static int[] testPapers; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + testPapers = new int[N]; + st = new StringTokenizer(br.readLine(), " "); + for(int i = 0; i < N; i++) { + testPapers[i] = Integer.parseInt(st.nextToken()); + totalScore += testPapers[i]; + } + + parametricSearch(); + + System.out.println(maxScore); + } + + public static void parametricSearch() { + int low = 0; + int high = totalScore; + + while(low <= high) { + int mid = (low + high) / 2; + + if(getGroupCount(mid) >= K) { + low = mid + 1; + maxScore = mid; + } else { + high = mid - 1; + } + } + } + + public static int getGroupCount(int score) { + int groupCount = 0; + int sumScore = 0; + + for(int testScore: testPapers) { + sumScore += testScore; + if(sumScore >= score) { + sumScore = 0; + groupCount++; + } + } + + return groupCount; + } + +} From 338e3bde07b1bd2daa11c7f83925e980012a8578 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Tue, 17 Oct 2023 20:29:02 +0900 Subject: [PATCH 08/10] [solved] boj3066 --- HuhJunHyeok/boj/boj3066.java | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 HuhJunHyeok/boj/boj3066.java diff --git a/HuhJunHyeok/boj/boj3066.java b/HuhJunHyeok/boj/boj3066.java new file mode 100644 index 00000000..ae01d747 --- /dev/null +++ b/HuhJunHyeok/boj/boj3066.java @@ -0,0 +1,54 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; + +/** + * [boj] 3066. 브리징 시그널 + */ +public class boj3066 { + /** + * T: 테스트 케이스의 수, N: 포트의 수 + */ + static int T, N; + static ArrayList lisPort; + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + T = Integer.parseInt(br.readLine()); + while(T-- > 0) { + lisPort = new ArrayList<>(); + + N = Integer.parseInt(br.readLine()); + while(N-- > 0) { + int portNum = Integer.parseInt(br.readLine()); + int idx = lisBinarySearch(portNum); + if(idx >= lisPort.size()) { + lisPort.add(portNum); + } else { + lisPort.set(idx, portNum); + } + } + sb.append(lisPort.size()).append("\n"); + } + System.out.print(sb.toString()); + } + + public static int lisBinarySearch(int portNum) { + int high = lisPort.size(); + int low = 0; + + while(low < high) { + int mid = (low + high) >> 1; + + if(lisPort.get(mid) < portNum) { + low = mid + 1; + } else { + high = mid; + } + } + + return high; + } +} From 8bc7279967c591579b1a07c6dcc163735a56c288 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Thu, 19 Oct 2023 16:39:52 +0900 Subject: [PATCH 09/10] [solved] boj15989 --- HuhJunHyeok/boj/boj15989.java | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 HuhJunHyeok/boj/boj15989.java diff --git a/HuhJunHyeok/boj/boj15989.java b/HuhJunHyeok/boj/boj15989.java new file mode 100644 index 00000000..ef5d619e --- /dev/null +++ b/HuhJunHyeok/boj/boj15989.java @@ -0,0 +1,44 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * [boj] 15989. 1, 2, 3 더하기 4 + */ +public class boj15989 { + /** + * n: 만들려고 하는 정수 + */ + static int n; + /** + * dp: dp 배열. dp[k][n]은 현재 n번 인덱스에서 사용하는 정수를 더할 때 k값을 만들 수 있는 경우의 수. + * 10_001 ==> 정수 n의 값이 10_000 이하의 양수 + * 3 ==> 1~3의 3가지 정수의 합. 0인덱스 1, 1인덱스 2, 2인덱스 3 + */ + static int[][] dp = new int[10_001][3]; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + dp[1][0] = 1; // 1 + dp[2][0] = 1; // 1 + 1 + dp[2][1] = 1; // 2 + dp[3][0] = 1; // 1 + 1 + 1 + dp[3][1] = 1; // 1 + 2 + dp[3][2] = 1; // 3 + for(int i = 4; i <= 10_000; i++) { + dp[i][0] = dp[i - 1][0]; + dp[i][1] = dp[i - 2][0] + dp[i - 2][1]; + dp[i][2] = dp[i - 3][0] + dp[i - 3][1] + dp[i - 3][2]; + } + + int T = Integer.parseInt(br.readLine()); + while(T-- > 0) { + n = Integer.parseInt(br.readLine()); + + sb.append(dp[n][0] + dp[n][1] + dp[n][2]).append("\n"); + } + + System.out.print(sb.toString()); + } +} From 0a937e1ec9cb020d588dbe487379dfe53fa1f335 Mon Sep 17 00:00:00 2001 From: ComelyU Date: Thu, 19 Oct 2023 16:40:19 +0900 Subject: [PATCH 10/10] [solved] boj2661 --- HuhJunHyeok/boj/boj2661.java | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 HuhJunHyeok/boj/boj2661.java diff --git a/HuhJunHyeok/boj/boj2661.java b/HuhJunHyeok/boj/boj2661.java new file mode 100644 index 00000000..aaf37838 --- /dev/null +++ b/HuhJunHyeok/boj/boj2661.java @@ -0,0 +1,65 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * [boj] 2661. 좋은수열 + */ +public class boj2661 { + /** + * MIN, MAX: 수열은 숫자 1~3으로만 이루어짐. + */ + static final int MIN = 1, MAX = 3; + /** + * N: 만들려는 수열의 길이 + */ + static int N; + /** + * findCheck: 가장 작은 수를 나타내는 수열을 찾았는가 + */ + static boolean findCheck; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + + simulate(new StringBuilder()); + } + + public static void simulate(StringBuilder sequence) { + if(findCheck) { + return; + } + + if(sequence.length() == N) { + System.out.println(sequence.toString()); + findCheck = true; + + return; +// System.exit(0); + } + + for(int nowNum = MIN; nowNum <= MAX; nowNum++) { + StringBuilder newSequence = new StringBuilder(sequence).append(nowNum); + + if(makeGoodSequence(newSequence)) { + simulate(newSequence); + } + } + } + + public static boolean makeGoodSequence(StringBuilder sequence) { + int sequenceLength = sequence.length(); + int halfSequenceLength = sequenceLength >> 1; + + for(int i = 1; i <= halfSequenceLength; i++) { // 확인할 문자열의 길이는 1 ~ 전체 문자열의 길이의 반 + String backPart = sequence.substring(sequenceLength - i, sequenceLength); // 맨 마지막 문자를 포함하는 i 길이의 문자열 + String frontPart = sequence.substring(sequenceLength - i * 2, sequenceLength - i); // backPart의 i 길이 앞 문자열 + + if(backPart.equals(frontPart)) { // 두 부분 수열이 동일하면 나쁜 수열 + return false; + } + } + return true; + } +}