1. 程式人生 > >712. Minimum ASCII Delete Sum for Two Strings(python+cpp)

712. Minimum ASCII Delete Sum for Two Strings(python+cpp)

題目:

Given two strings s1, s2, find the lowest ASCII sum of deletedcharacters to make two strings equal.
Example 1:

Input: s1 = "sea", s2 = "eat" 
Output: 231 
Explanation:
Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum. 
At the end, both stringsare equal, and 115 + 116 = 231 is the minimum sum possible to achieve this. 

Example 2:

Input: s1 = "delete", s2 = "leet" 
Output: 403
Explanation: 
Deleting "dee" from "delete" to turn the string into"let", adds 100[d]+101[e]+101[e] to the sum.  
Deleting "e" from "leet" adds 101[e] to the sum. At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403. 
If instead we turned both
strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

Note:
0 < s1.length, s2.length <= 1000.
All elements of each string will have an ASCII value in [97, 122].

解釋:
動態規劃
dp[i][j]表示s1的前i個字母和s2的前j個字母變成一樣所需要的最小ASCII和,則一共有三種情況
1.dp[i-1][j]+s1[i],從dp[i-1][j]dp[i][j]是多考慮了s1的一個字元,但是s2的字元數沒有變,所以想要相同的話必須刪除s1[i],考慮ASCII的話就是加上s1[i]的ASCII
2.dp[i][j-1]+s2[j]

,刪除s2[j]
3.dp[i-1][j-1]+a ,如果s1[i]==s2[j],則a=0,如果s1[i]!=s2[j],則a=s1[i]+s2[j]
4.在上述三種情況中找最小值
5.這種方法在python中超時了,反正速度特別慢,還是用下面的方法吧:
6.最長公共子序列解法:所有的減去2*相等的字元的ASCII的最大值(即最長公共子序列LCS),等於題目所需的最小值
最長公共子序列只能求長度,如何求具體的元素?
答:求lcs(subsequence)的時候不是+1而是+ord(s[i]) 最後返回的是dp[m][n]而不是maxLen。(lcsubarray和lcsubsequence的寫法略有不同)
python程式碼:

class Solution(object):
    def minimumDeleteSum(self, s1, s2):
        """
        :type s1: str
        :type s2: str
        :rtype: int
        """
        def lcsubsequence(s1,s2):
            m=len(s1)
            n=len(s2)
            dp=[[0]*(n+1) for _ in xrange(m+1)]
            for i in xrange(1,m+1):
                for j in xrange(1,n+1):
                    if s1[i-1]==s2[j-1]:
                        dp[i][j]=dp[i-1][j-1]+ord(s1[i-1])
                    else:
                        dp[i][j]=max(dp[i-1][j],dp[i][j-1])
            return dp[m][n]
        m=len(s1)
        n=len(s2)
        if m==0 and n==0:
            return 0
        if m==0:
            return sum(map(ord,s2))
        if n==0:
            return sum(map(ord,s1))
        return sum(map(ord,s1+s2))-2*lcsubsequence(s1,s2)

c++程式碼:

class Solution {
public:
    int minimumDeleteSum(string s1, string s2) {
        int m=s1.size(),n=s2.size();
        if (m==0 && n==0)
            return 0;
        if (m==0)
            return accumulate(s2.begin(),s2.end(),0);
        if (n==0)
            return accumulate(s1.begin(),s1.end(),0);
        return accumulate(s1.begin(),s1.end(),0)+accumulate(s2.begin(),s2.end(),0)-2*subsequence(s1,s2);
    };
    int subsequence(string s1,string s2)
    {
        int m=s1.size(),n=s2.size();
        vector<vector<int>>dp(m+1,vector<int>(n+1,0));
        for (int i=1;i<=m;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if(s1[i-1]==s2[j-1])
                    dp[i][j]=dp[i-1][j-1]+s1[i-1];
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[m][n];
    }
};

總結:
這道題和
718. Maximum Length of Repeated Subarray(python+cpp)以及 583. Delete Operation for Two Strings(python+cpp)一起學習,718是求lcsubarray,583是利用了lcsubsequence。
其實對於mn是否為0的判斷是不必要的,因為題目已經限定了字串不為空,但是python中刪掉判斷居然變慢了很多,c++倒是不變,為什麼???不過為了以後lcs的應用,還是把判斷加上吧…