Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAXN 1001
- using namespace std;
- char s[MAXN];
- int n;
- int max (int x, int y) { return (x > y)? x : y; }
- int lps(char *str, int n)
- {
- int i, j, cl;
- int L[n][n];
- for (i = 0; i < n; i++)
- L[i][i] = 1;
- for (cl=2; cl<=n; cl++)
- {
- for (i=0; i<n-cl+1; i++)
- {
- j = i+cl-1;
- if (str[i] == str[j] && cl == 2)
- L[i][j] = 2;
- else if (str[i] == str[j])
- L[i][j] = L[i+1][j-1] + 2;
- else
- L[i][j] = max(L[i][j-1], L[i+1][j]);
- }
- }
- return L[0][n-1];
- }
- int main()
- {
- cin>>n;
- cin.get();
- cin.getline(s, MAXN);
- cout<<lps(s, n);
- return 0;
- }
Add Comment
Please, Sign In to add comment