Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int PalinPart(string str){
  7. int n=str.length();
  8. bool isP[n][n];
  9. for(int i=0;i<n;i++){
  10. for(int j=0;j<n;j++){
  11. isP[i][j]=false;
  12.  
  13. if( (i==j) || ( (i)==(j+1) ))
  14. isP[i][j]=true;
  15. }
  16. }
  17. // Fill the isP Matrix to mark all palindrome substrings
  18. int length=1;
  19. while(length!=n){
  20. for(int i=0,j=length;((i<n)&&(j<n));i++,j++){
  21. if( (str[i]==str[j]) && isP[i+1][j-1])
  22. isP[i][j]=true;
  23. }
  24. length++;
  25. }
  26. // Printing the boolean matrix
  27. // for(int i=0;i<n;i++){
  28. // for(int j=0;j<n;j++){
  29. // cout<<isP[i][j]<<" ";
  30. // }
  31. // cout<<endl;
  32. // }
  33.  
  34. // Now Apply DP again to find minimum number of cuts
  35. int count[n]={INT_MAX};
  36. for(int i=0;i<n;i++){
  37. if(isP[0][i]){
  38. count[i]=0;
  39. }else{
  40. count[i]=INT_MAX;
  41. for(int j=0;j<i;j++){
  42. if(isP[j+1][i] && ((1+count[j])<count[i]) )
  43. count[i]=1+count[j];
  44. }
  45. }
  46. }
  47. return count[n-1];}
  48.  
  49. int main(){
  50.  
  51. int tc;
  52. cin>>tc;
  53. while(tc--){
  54. string str;
  55. cin>>str;
  56.  
  57. cout<<PalinPart(str)<<endl;
  58.  
  59.  
  60. }
  61.  
  62.  
  63.  
  64. return 0;}
Add Comment
Please, Sign In to add comment