From e3810cf63c8666edb62a46275f3399f15e27ab96 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sun, 13 Jul 2025 17:39:53 +0900 Subject: [PATCH] =?UTF-8?q?[20250713]=20BOJ=20/=20G1=20/=20=ED=8C=B0?= =?UTF-8?q?=EB=A6=B0=EB=93=9C=EB=A1=AC=20=EB=B6=84=ED=95=A0=20/=20?= =?UTF-8?q?=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\241\254 \353\266\204\355\225\240.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "lkhyun/202507/13 BOJ G1 \355\214\260\353\246\260\353\223\234\353\241\254 \353\266\204\355\225\240.md" diff --git "a/lkhyun/202507/13 BOJ G1 \355\214\260\353\246\260\353\223\234\353\241\254 \353\266\204\355\225\240.md" "b/lkhyun/202507/13 BOJ G1 \355\214\260\353\246\260\353\223\234\353\241\254 \353\266\204\355\225\240.md" new file mode 100644 index 00000000..6b2289b7 --- /dev/null +++ "b/lkhyun/202507/13 BOJ G1 \355\214\260\353\246\260\353\223\234\353\241\254 \353\266\204\355\225\240.md" @@ -0,0 +1,52 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static boolean[][] dp; + static int[] dp2; + + public static void main(String[] args) throws Exception{ + String str = br.readLine(); + int len = str.length(); + dp = new boolean[len][len]; + + for (int i = 0; i < len; i++) { // 한 자리 + dp[i][i] = true; + } + for (int i = 0; i < len-1; i++) { // 두 자리 + if(str.charAt(i) == str.charAt(i+1)) { + dp[i][i+1] = true; + } + } + + for (int i = 2; i < len; i++) { // 그외 + for (int j = 0; j+i < len; j++) { + if(str.charAt(j) == str.charAt(i+j)){ + dp[j][i+j] = dp[j+1][i+j-1]; + }else{ + dp[j][i+j] = false; + } + } + } + + dp2 = new int[len+1]; //0부터 시작해서 i까지의 문자까지 보았을 때 팰린드롬으로 분할하는 최소 개수 + Arrays.fill(dp2, Integer.MAX_VALUE); + dp2[0] = 0; + + for (int i = 1; i <= len; i++) { //모든 길이에 대해서 + for (int j = 0; j < i; j++) { //첫번째 문자부터 보기 + if(dp[j][i-1]){ //j지점에서 문자열 끝까지 팰린드롬이면 + dp2[i] = Math.min(dp2[i], dp2[j] + 1); //j지점까지의 분할 개수와 j지점 이후는 팰린드롬이므로 1을 더해주기 + } + } + } + + bw.write(dp2[len] + ""); + bw.close(); + } +} +```