a53

SubsirPalindromMaximal

a53
May 16th, 2021 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAXN 1001
  3. using namespace std;
  4. char s[MAXN];
  5. int n;
  6.  
  7. int max (int x, int y) { return (x > y)? x : y; }
  8.  
  9. int lps(char *str, int n)
  10. {
  11. int i, j, cl;
  12. int L[n][n];
  13. for (i = 0; i < n; i++)
  14. L[i][i] = 1;
  15. for (cl=2; cl<=n; cl++)
  16. {
  17. for (i=0; i<n-cl+1; i++)
  18. {
  19. j = i+cl-1;
  20. if (str[i] == str[j] && cl == 2)
  21. L[i][j] = 2;
  22. else if (str[i] == str[j])
  23. L[i][j] = L[i+1][j-1] + 2;
  24. else
  25. L[i][j] = max(L[i][j-1], L[i+1][j]);
  26. }
  27. }
  28.  
  29. return L[0][n-1];
  30. }
  31.  
  32. int main()
  33. {
  34. cin>>n;
  35. cin.get();
  36. cin.getline(s, MAXN);
  37. cout<<lps(s, n);
  38. return 0;
  39. }
  40.  
Add Comment
Please, Sign In to add comment