import java.io.IOException;
import java.util.Scanner;
public class Main {
public static int n;
public static char[] chs;
public static int[][] cache;
//s[i...j]变成回文串需要加的最少的字符
public static int dp(int i,int j){
//base
if(i==j)
return 0;
//cache
if(cache[i][j]!=Integer.MAX_VALUE)
return cache[i][j];
//dp
if(chs[i]==chs[j]){
if(i+1<=n-1&&j-1>=0)
cache[i][j]=dp(i+1,j-1);
}else{
if(i+1<=n-1&&j-1>=0)
cache[i][j]=Math.min(dp(i+1,j),dp(i,j-1))+1;
}
return cache[i][j];
}
public static void main(String[] args) throws IOException {
//init
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
String s = sc.next();
chs = s.toCharArray();
cache = new int[n+1][n+1];
for(int i=0;i<n;i++){
for(int j=i;j<n;j++)
cache[i][j]=Integer.MAX_VALUE;
}
//res
System.out.println(dp(0,n-1));
}
}
poj-1159-Palindrome
最新推荐文章于 2022-05-16 16:41:42 发布