-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPalindromic patitioning.cpp
More file actions
49 lines (47 loc) · 1.21 KB
/
Palindromic patitioning.cpp
File metadata and controls
49 lines (47 loc) · 1.21 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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
const int N = 1e3;
int minCut(string s) {
int palin[N][N];
int n = s.size();
memset(palin ,0 , sizeof(palin));
for(int i = 0 ; i < n ; i++)
palin[i][i] = 1;
for(int gap = 2 ;gap <= n ;gap++){
for(int i = 0 ; i < n - gap + 1; i++){
int j = i + gap - 1;
if(gap == 2)
palin[i][j] = (s[i] == s[j]);
else if((s[i] == s[j]) && palin[i + 1][j - 1])
palin[i][j] = 1;
}
}
int ans[N];
memset(ans , 0 , sizeof(ans));
for(int i = 0 ; i < n ; i++){
if(palin[0][i])
ans[i] = 0;
else{
ans[i] = INT_MAX;
for(int j = 0 ;j < i; j++){
if(ans[i] > ans[j] + 1 and palin[j + 1][i])
ans[i] = ans[j] + 1;
}
}
}
return ans[n - 1];
}
};
int main() {
int t ;
scanf("%d" , &t);
while(t--){
string ss;
cin >> ss;
Solution obj;
cout << obj.minCut(ss) << endl;
}
return 0;
}