题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
LeetCode上原题,最长回文子串,动态规划解法
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
boolean[][] dp = new boolean[s.length()][s.length()];
for (int i = s.length() - 1; i >= 0; i--) {
for (int j = i; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
if (j - i < 2) dp[i][j] = true;
else dp[i][j] = dp[i + 1][j - 1];
}
}
}
int max = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < s.length(); j++) {
if (dp[i][j]) max = Math.max(max, j - i + 1);
}
}
System.out.println(max);
}
}