The link of the question is https://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence/0 . May I know whether my approach is a correct solution using Dynamic Programming . The code is :
#include <bits/stdc++.h>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--){
string a;
cin>>a;
int l = a.length();
int dp[l][l] = {0};
for(int i = 0; i<l; i++){
for(int j = 0; j<l; j++){
if(i==j){
dp[i][i] = 1;
}
}
}
for(int i = 1; i<l; i++){
int r = 0;
int c = i;
while(r<l and c<l){
if(a[r]==a[c]){
dp[r][c] = dp[r+1][c-1]+2;
}
else{
dp[r][c] = max(dp[r+1][c], dp[r][c-1]);
}
r++;
c++;
}
}
cout<<dp[0][l-1]<<endl;
}
return 0;
}
where t is the number of test cases and a is the input string . We have to determine the length of the longest palindromic subsequence .