-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_part_mem_opt.cpp
More file actions
60 lines (58 loc) · 1.24 KB
/
pal_part_mem_opt.cpp
File metadata and controls
60 lines (58 loc) · 1.24 KB
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
#include<iostream>
#include<string.h>
using namespace std;
int static t[1000][1000];
class mcm
{
public:
bool palindrome(string st,int i,int j)
{
while(i<j)
{
if(st[i]!=st[j-1])
return false;
else
i++;
j--;
}
return true;
}
int pal_part_optimized(string s,int x,int y)
{
if(x>=y)
return 0;
if(palindrome(s,x,y)==true)
return 0;
if(t[x][y]!=-1)
return t[x][y];
int left,right,temp,mn = INT_MAX;
for(int k=x;k<=y-1;k++)
{
if(t[x][k]!=-1)
left = t[x][k];
else
{
left = pal_part_optimized(s,x,k);
t[x][k] = left;
}
if(t[k+1][y]!=-1)
right = t[k+1][y];
else
{
right = pal_part_optimized(s,k+1,y);
t[k+1][y] = right;
}
temp = 1 + left + right;
if(temp<mn)
mn = temp;
}
return t[x][y] = mn;
}
};
int main()
{
string s = "abcabcbacdefefd";
memset(t,-1,sizeof(t));
mcm ob;
cout<<ob.pal_part_optimized(s,0,s.length());
}