-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132.palindrome-partitioning-ii.160157047.ac.cpp
56 lines (51 loc) · 1.41 KB
/
132.palindrome-partitioning-ii.160157047.ac.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
class Solution {
public:
int minCut(string s) {
cout<<s.length()<<"v";
int n = s.length();
/*TLE
int v[n][n];
for(int l=1;l<=n;l++)
{
for(int i=0;i<n-l+1;i++)
{
int j=i+l-1;
if(i==j)v[i][j]=0;
else if(j==i+1)
{
if(s[i]==s[j])v[i][j]=0;
else v[i][j]=1;
}
else
{
if(s[i]==s[j]&&v[i+1][j-1]==0)
v[i][j]=v[i+1][j-1];
else
{
v[i][j]=INT_MAX;
for(int p = i;p<j;p++)
{
v[i][j]=min(v[i][j],v[i][p]+v[p+1][j]+1);
}
}
}
}
}
*/
vector<int > cuts(n+1);
for(int i=0;i<=s.length();i++)cuts[i]=i-1;
for(int i=0;i<=s.length();i++)
{
//odd length
for(int j=0;i-j>=0&&i+j<n&&s[i-j]==s[i+j];j++)
{
cuts[i+j+1]=min(cuts[i+j+1],cuts[i-j]+1);
}
for(int j=0;i-j+1>=0&&i+j<n&&s[i-j+1]==s[i+j];j++)
{
cuts[i+j+1]=min(cuts[i-j+1]+1,cuts[i+j+1]);
}
}
return cuts[n];
}
};