-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_partition.cpp
More file actions
46 lines (43 loc) · 949 Bytes
/
palindrome_partition.cpp
File metadata and controls
46 lines (43 loc) · 949 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
string str;
cin>>str;
int n=str.size();
int cost[n];
bool pali[n][n];
for(int i=0;i<n;++i){
pali[i][i]=true;
}
for(int l=2;l<=n;++l){
for(int i=0;i<n-l+1;++i){
int j=i+l-1;
if(l==2){
pali[i][j] = (str[i]==str[j]);
}
else{
pali[i][j] = (str[i]==str[j]) && pali[i+1][j-1];
}
}
}
for(int i=0;i<n;++i){
if(pali[0][i]){
cost[i]=0;
}
else{
cost[i]=INT_MAX;
for(int k=0;k<i;++k){
if(pali[k+1][i]){
cost[i]=min(cost[i], cost[k]+1);
}
}
}
}
cout<<cost[n-1]<<endl;
}
return 0;
}