Longest Palindromic Subsequence
import java.util.Scanner;
class Solve{
public int cal(String a, String b, int n){
int t[][]=new int[n+1][n+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<n+1;j++){
if(i==0 || j==0){
t[i][j]=0;
}
}
}
for(int i=01;i<n+1;i++){
for(int j=01;j<n+1;j++){
if(a.charAt(i-1)==b.charAt(j-1)){
t[i][j]=1+t[i-1][j-1];
}
else{
t[i][j]=Integer.max(t[i-1][j],t[i][j-1]);
}
}
}
return t[n][n];
}
}
class LongestPaindromicSubsequence{
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
String s=sc.next();
StringBuilder sb=new StringBuilder();
sb.append(s);
String b=(sb.reverse().toString());
int n=s.length();
Solve obj=new Solve();
int c=obj.cal(s,b,n);
System.out.println(c);
}
}
Comments
Post a Comment