-
Notifications
You must be signed in to change notification settings - Fork 0
/
1509.cpp
61 lines (55 loc) · 1.26 KB
/
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
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
#include <bits/stdc++.h>
using namespace std;
string str;
int len;
bool dp[2500][2500];
// bool isPalindrome(){
// }
vector<int> countDP;
void expand(int start, int end){
if(start-1 < 0 || end+1 > len-1){
return;
}
if(str[start-1] == str[end+1]){
dp[start-1][end+1] = true;
expand(start-1, end+1);
}
}
int main(void){
cin >> str;
len = str.length();
for(int i=0; i<len; i++){
dp[i][i] = true;
}
for(int i=0; i<len-1; i++){
if(str[i] == str[i+1]){
dp[i][i+1] = true;
}
}
for(int i=0; i<len; i++){
for(int j=0; j<len; j++){
if(dp[i][j]){
expand(i, j);
}
}
}
int ans = INT_MAX;
countDP.resize(len);
countDP.assign(len, INT_MAX);
countDP[0] = 1;
for(int i=1; i<len; i++){
countDP[i] = countDP[i-1] + 1;
for(int j=0; j<=i; j++){
if(dp[j][i])
if(j==0){
countDP[i] = 1;
}
else if(countDP[i] > (1+countDP[j-1])){
countDP[i] = 1 + countDP[j-1];
ans = 1 + countDP[j-1];
}
}
}
cout << countDP[len-1] << endl;
return 0;
}