字符串转换; s-> b 需要转换最少字符数 ;
#include <cstring> #include <iostream> int min(int a,int b){ return a>b? b:a; } int dp[1010][1010]; int main() { char s[1010], b[1010]; while(std::cin >> s>> b) { int n=strlen(s); int m=strlen(b); for(int i=0; i<=n; i++) dp[i][0]= i; for(int i=0; i<=m; i++) dp[0][i]= i; for(int i=1; i<= n; i++) for(int j=1; j<= m; j++) { dp[i][j]=min(dp[i-1][j], dp[i][j-1])+ 1; if(s[i-1]== b[j-1]) dp[i][j]=min(dp[i][j], dp[i-1][j-1]); else dp[i][j]=min(dp[i][j], dp[i-1][j-1]+1); } printf("%d ", dp[n][m]); } return 0; }