Skip to content

Instantly share code, notes, and snippets.

@bourneagain
Created August 18, 2015 06:17
Show Gist options
  • Save bourneagain/fb7cc841112e24cce19c to your computer and use it in GitHub Desktop.
Save bourneagain/fb7cc841112e24cce19c to your computer and use it in GitHub Desktop.
public void cal(char[] str){
System.out.println(str.length);
int dp[][] = new int[str.length+1][str.length+1];
for (int i=0; i<str.length; i++){
dp[i][i] = 1;
}
int lpsLong = Integer.MIN_VALUE;
for (int c=2; c<=str.length; c++){
for (int i=0; i<=str.length-c; i++){
int end = i+c-1;
if (str[i] != str[end]){
dp[i][end] = Math.max(dp[i][end-1], dp[i+1][end]);
} else if (str[i] == str[end]){
dp[i][end] = 2 + dp[i+1][end-1];
}
System.out.println("c " + c + " " +i+":"+end+"=>"+(new String(str)).substring(i,end+1)+" = "+dp[i][end]);
lpsLong = Math.max(dp[i][end],lpsLong);
}
System.out.println("-----");
}
System.out.println(lpsLong);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment