class Solution {
public:
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    int minEditCost(string str1, string str2, int ic, int dc, int rc) {
        // write code here
        int l1 = str1.size(),l2 = str2.size();
        int dp[l2+1];
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=l2;i++){
            dp[i] = ic*i;
        }
        for(int i=1;i<=l1;i++){
            int pre = dp[0];   //记录dp[i-1][0]
            dp[0] = dc*i;      //手动更新dp[i][0]值
            for(int j=1;j<=l2;j++){
                int tem = dp[j];
                if(str1[i-1] == str2[j-1]){
                    dp[j] = pre;
                }else{
                    dp[j] = min(dp[j-1] + ic,pre + rc);
                    dp[j] = min(dp[j], tem + dc);
                }
                pre = tem;
            }
        }
        
        return dp[l2];
    }
};