forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromic_partitioning_recursive.cpp
66 lines (53 loc) · 1.59 KB
/
palindromic_partitioning_recursive.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
57
58
59
60
61
62
63
64
65
66
/*
Given a string, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. For example, “aba|b|bbabb|a|b|aba” is a palindrome partitioning of “ababbbabbababa”. Determine the fewest cuts needed for palindrome partitioning of a given string. For example, minimum 3 cuts are needed for “ababbbabbababa”. The three cuts are “a|babbbab|b|ababa”.
Input:
The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. The first line of every Test Case consists of S, denoting a String.
Output:
For each test case in a new line print an integer, denoting the number cuts in the String to make it palindromic.
Constraints:
1<=T<=100
1<=|Length of String|<=1000
Example:
Input:
2
ababbbabbababa
aaabba
Output:
3
1
*/
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string s, int i, int j){
if(i==j) return true;
if(i>j) return true;
while(i<j){
if(s[i]!=s[j]) return false;
i++; j--;
}
return true;
}
int palindromicPartion(string s, int i, int j){
if(i>=j) return 0;
if(isPalindrome(s, i, j)) return 0;
int partition=INT_MAX;
for(int k=i;k<j;k++){
int tmp = 1 + palindromicPartion(s, i, k) + palindromicPartion(s, k+1, j);
if(tmp<partition) partition=tmp;
}
return partition;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
string s;
cin>>s;
int n=s.length();
cout<<palindromicPartion(s,0,n-1)<<endl;
}
return 0;
}