题解 | #最长回文子序列#
最长回文子序列
https://www.nowcoder.com/practice/82297b13eebe4a0981dbfa53dfb181fa
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
// in.nextLine();
String str = in.nextLine();
int len=str.length();
int[][] dp= new int[len][len];
long res=Long.MIN_VALUE;
for(int i=0;i<len;i++){
dp[i][i]=1;
}
for(int i=len-1;i>=0;i--){
for(int j=i+1;j<len;j++){
if(str.charAt(i) ==str.charAt(j))dp[i][j] = dp[i+1][j-1] + 2;
else dp[i][j] = Math.max(dp[i][j-1],dp[i+1][j]);
}
}
System.out.println(dp[0][len-1]);
}
}
}

