leetcode712+減少最少的字母湊成相同序列,最長公共子序列變形,DP
阿新 • • 發佈:2018-11-09
https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/
class Solution { public: int minimumDeleteSum(string s1, string s2) { string res = ""; int dp[s1.length()+1][s2.length()+1]; memset(dp, 0, sizeof(dp)); int sum = 0; for(int i=0; i<s1.length(); i++){ sum += (int)s1[i]; for(int j=0; j<s2.length(); j++){ if(s1[i]==s2[j]){ dp[i+1][j+1] = dp[i][j] + (int)s1[i]; } else{ dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]); } } } for(int j=0; j<s2.length(); j++) sum+=(int)s2[j]; return sum -2*dp[s1.length()][s2.length()]; } };