[BOJ] 1509 펠린드롬 분할

Time Lapse :24min 1sec

1509.cpp

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
#include <iostream>
#include <memory.h>
using namespace std;
bool pal[2501][2501];
short dp[2501];
int len;
string s;
void func(int f, int e) {
pal[f][e] = true;
if (f && e < len && s[f - 1] == s[e + 1])
func(f - 1, e + 1);
}

short dpf(int n) {
if(n < 0) return 0;
if(dp[n] != -1) return dp[n];
dp[n] = dpf(n-1);
for(int i = n - 1; i >= 0; --i)
if(pal[i][n])
dp[n] = min(dp[n], dpf(i - 1));
return ++dp[n];
}

int main(void) {
cin>>s;
len = s.length();
for (int i = 0; i <len; ++i) {
func(i, i);
if(s[i] == s[i+1])
func(i, i + 1);
}
memset(dp, -1, sizeof(dp));
printf("%d",dpf(len - 1));
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/22/PS/BOJ/1509/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.