动态规划
最优化原理:
无后向性:
public int EditDistance1(String src ,String dest){ if( src.length() == 0 || dest.length() == 0 ){ return Math.abs(src.length() - dest.length()); } if( src.charAt(0) == dest.charAt(0) ){ return EditDistance1(src.substring(1),dest.substring(1)); } int edIns = EditDistance1(src,dest.substring(1))+1; int edDel = EditDistance1(src.substring(1),dest)+1; int edRep = EditDistance1(src.substring(1),dest.substring(1))+1; return Math.min(Math.min(edIns, edDel), edRep); }
public int EditDistance1(String src ,String dest){ if( src.length() == 0 || dest.length() == 0 ){ return Math.abs(src.length() - dest.length()); } if( src.charAt(0) == dest.charAt(0) ){ return EditDistance1(src.substring(1),dest.substring(1)); } int edIns = EditDistance1(src,dest.substring(1))+1; int edDel = EditDistance1(src.substring(1),dest)+1; int edRep = EditDistance1(src.substring(1),dest.substring(1))+1; return Math.min(Math.min(edIns, edDel), edRep); }
public int EditDistance1(String src ,String dest){ if( src.length() == 0 || dest.length() == 0 ){ return Math.abs(src.length() - dest.length()); } if( src.charAt(0) == dest.charAt(0) ){ return EditDistance1(src.substring(1),dest.substring(1)); } int edIns = EditDistance1(src,dest.substring(1))+1; int edDel = EditDistance1(src.substring(1),dest)+1; int edRep = EditDistance1(src.substring(1),dest.substring(1))+1; return Math.min(Math.min(edIns, edDel), edRep); }