madhu242

Palindrome - DP

Oct 25th, 2022 (edited)
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | Source Code | 0 0
  1. // Palindromic Subsequence - DP
  2.  
  3. // Longest palindromic subsequence of a string
  4. using namespace std;
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. class LPS {
  11.  
  12. public:
  13.   int findLPSLength(const string &st) {
  14.     vector<vector<int>> dp(st.length(), vector<int>(st.length(), 0));
  15.     // every sequence with one element is a palindrome of length 1
  16.     for (int i = 0; i < st.length(); i++) {
  17.       dp[i][i] = 1;
  18.     }
  19.  
  20.     for (int l = 1; l < st.size(); l++) { // take length of string as a factor and findout best
  21.     // palindromic substring with that length
  22.       for (int j = l; j < st.size(); j++) {
  23.         // 2 + best palindromic string in between them
  24.         if (st[j-l] == st[j]) {
  25.           dp[j-l][j] = 2 + dp[j-l+1][j-1];
  26.         } else {
  27.           // take maximum either including this character or excluding this character
  28.           // inorder to maintain string length, check different matrices.
  29.           dp[j-l][j] = max(dp[j-l+1][j], dp[j-l][j-1]);
  30.         }
  31.       }
  32.     }
  33.     return dp[0][st.size() - 1];
  34.   }
  35. };
  36.  
  37. int main(int argc, char *argv[]) {
  38.   LPS *lps = new LPS();
  39.   cout << lps->findLPSLength("abdbca") << endl;
  40.   cout << lps->findLPSLength("cddpd") << endl;
  41.   cout << lps->findLPSLength("pqr") << endl;
  42.  
  43.   delete lps;
  44. }
  45.  
  46. // Longest palindromic substring length
  47. class LPS {
  48.  
  49. public:
  50.   int findLPSLength(const string &st) {
  51.     // dp[i][j] will be 'true' if the string from index 'i' to index 'j' is a palindrome
  52.     vector<vector<bool>> dp(st.length(), vector<bool>(st.length()));
  53.  
  54.     // every string with one character is a palindrome
  55.     for (int i = 0; i < st.length(); i++) {
  56.       dp[i][i] = true;
  57.     }
  58.  
  59.     int maxLength = 1;
  60.     for (int l = 1; l < st.length(); l++) {
  61.       for (int j = l; j < st.length(); j++) {
  62.         if (st[j-l] == st[j]) {
  63.           // if it's a two character string or if the remaining string is a palindrome too
  64.           if (dp[j - l + 1][j - 1]) {
  65.             dp[j-l][j] = true;
  66.             maxLength = max(maxLength, l + 1);
  67.           }
  68.         }
  69.       }
  70.     }
  71.  
  72.     return maxLength;
  73.   }
  74. };
  75.  
  76. // Count of palindromic substrings
  77. class CPS {
  78. public:
  79.   int findCPS(const string &st) {
  80.     // dp[i][j] will be 'true' if the string from index 'i' to index 'j' is a palindrome
  81.     vector<vector<bool>> dp(st.length(), vector<bool>(st.length()));
  82.     int count = 0;
  83.  
  84.     // every string with one character is a palindrome
  85.     for (int i = 0; i < st.length(); i++) {
  86.       dp[i][i] = true;
  87.       count++;
  88.     }
  89.  
  90.     for (int l = 1; l < st.size(); l++) {
  91.       for (int j = l; j < st.length(); j++) {
  92.         if (st[j-l] == st[j]) {
  93.           // if it's a two character string or if the remaining string is a palindrome too
  94.           if (dp[j-l + 1][j - 1]) {
  95.             dp[j-l][j] = true;
  96.             count++;
  97.           }
  98.         }
  99.       }
  100.     }
  101.  
  102.     return count;
  103.   }
  104. };
  105.  
  106. // Minimum deletions/additions to make a string palindrome
  107.  
  108. Minimum_deletions_to_make_palindrome = Length(st) - LongestPalindromicSubsequence(st)
  109.  
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment