baekjoon 20251214
This commit is contained in:
parent
e8636c6b99
commit
80a44165ae
43
code_study/Baekjoon/java/_1509.java
Normal file
43
code_study/Baekjoon/java/_1509.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.util.*;
|
||||
|
||||
public class _1509 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String str = sc.nextLine();
|
||||
sc.close();
|
||||
|
||||
int L = str.length();
|
||||
boolean[][] isPalindrome = new boolean[L][L];
|
||||
for(int i=0; i<L; i++) {
|
||||
isPalindrome[i][i] = true;
|
||||
|
||||
if(i < L-1 && str.charAt(i) == str.charAt(i+1)) {
|
||||
isPalindrome[i][i+1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for(int len=3; len<=L; len++) {
|
||||
for(int start=0; start <= L - len; start++) {
|
||||
int end = start + len - 1;
|
||||
|
||||
if(str.charAt(start) == str.charAt(end) && isPalindrome[start+1][end-1]) {
|
||||
isPalindrome[start][end] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int[] dp = new int[L];
|
||||
Arrays.fill(dp, Integer.MAX_VALUE);
|
||||
|
||||
for(int end=0; end<L; end++) {
|
||||
for(int start=0; start<=end; start++) {
|
||||
if(isPalindrome[start][end]) {
|
||||
if(start==0) dp[end] = 1;
|
||||
else dp[end] = Math.min(dp[end], dp[start-1] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(dp[L-1]);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user