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