-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_part_rec.cpp
More file actions
47 lines (46 loc) · 902 Bytes
/
pal_part_rec.cpp
File metadata and controls
47 lines (46 loc) · 902 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
47
#include<iostream>
#include<string.h>
using namespace std;
class mcm
{
public:
bool is_palindrome(string st,int x,int y)
{
if(x==y)
return true;
if(x>y)
return true;
while(x<y)
{
if(st[x]!=st[y-1])
return false;
else
{
x++;
y--;
}
}
return true;
}
int pal_part_rec(string s,int i,int j)
{
if(i>=j)
return 0;
if(is_palindrome(s,i,j) == true)
return 0;
int mn = INT_MAX;
for(int k=i;k<=j-1;k++)
{
int temp = 1 + pal_part_rec(s,i,k) + pal_part_rec(s,k+1,j);
if(temp<mn)
mn = temp;
}
return mn;
}
};
int main()
{
string pal = "abcabcbacdefefd";
mcm ob;
cout<<ob.pal_part_rec(pal,0,pal.length());
}