-
Notifications
You must be signed in to change notification settings - Fork 2
/
1044 - Palindrome Partitioning.cpp
60 lines (58 loc) · 1.33 KB
/
1044 - Palindrome Partitioning.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
// https://sarwar05.blogspot.com/2018/06/1044-palindrome-partitioning.html
#include<bits/stdc++.h>
using namespace std;
#define inf 10000007
char str[1003];
int dp[1002];
vector<int> Next[1002];
int len;
int fun(int now)
{
if(now==len) return 0;
if(dp[now]!=-1) return dp[now];
int ret = inf;
for(int i=0; i<Next[now].size(); i++){
ret = min(ret,1+fun(Next[now][i]));
}
return dp[now] = ret;
}
void process()
{
for(int i=0; i<=len; i++) Next[i].clear();
for(int center = 0; center<len; center++){/// for odd length palindrome
int i,j;
i=j=center;
while(str[i]==str[j])
{
Next[i].push_back(j+1);
i--;
j++;
if(i<0 || j==len) break;
}
}
for(int center = 0; center<len; center++){/// for even length palindrome
int i,j;
i=j=center;
j++;
while(str[i]==str[j])
{
Next[i].push_back(j+1);
i--;
j++;
if(i<0 || j==len) break;
}
}
}
int main()
{
int tc, cs=1;
scanf("%d", &tc);
while(tc--){
scanf("%s",str);
len = strlen(str);
process();
memset(dp, -1, sizeof dp);
printf("Case %d: %d\n",cs++,fun(0));
}
return 0;
}