-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome Partitioning.cpp
More file actions
46 lines (41 loc) · 996 Bytes
/
Palindrome Partitioning.cpp
File metadata and controls
46 lines (41 loc) · 996 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 palinPart(string);
int main(){
int t;
string str;
cin >> t;
while(t--){
cin >> str;
cout << palinPart(str) << endl;
}
return 0;
}
int palinPart(string str){
int n = str.length();
int arr[n];
bool isPal[n][n];
for(int i = 0; i < n; i++)
isPal[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)
isPal[i][j] = (str[i] == str[j]);
else
isPal[i][j] = (str[i] == str[j]) && isPal[i + 1][j - 1];
}
}
for(int i = 0; i < n; i++){
if(isPal[0][i] == true)
arr[i] = 0;
else{
arr[i] = INT_MAX;
for(int j = 0; j < i; j++){
if(isPal[j + 1][i] == true && 1 + arr[j] < arr[i])
arr[i] = 1 + arr[j];
}
}
}
return arr[n - 1];
}