参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String in = scanner.nextLine(); int n = in.length(); boolean [][]dp = new boolean[n][n]; for(int i = 0; i < n; i++) dp[i][i] = true; for(int len = 2; len <= n; len++) { for(int i = 0; i < n; i++) { int j = i + len - 1; if(j >= n) break; if(in.charAt(i) == in.charAt(j)) { if(j - i < 3) dp[i][j] = true; else dp[i][j] = dp[i + 1][j - 1]; } if(dp[i][j]) System.out.println(in.substring(i, j + 1)); } } } }
0.0分
6 人评分