[LeetCode]712. Minimum ASCII Delete Sum for Two Strings
来源:互联网 发布:多功能水枪 数据 编辑:程序博客网 时间:2024/06/07 12:50
- descrption
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. - Example 1
Input: s1 = "sea", s2 = "eat"Output: 231Explanation: 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 strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
- Example 2
Input: s1 = "delete", s2 = "leet"Output: 403Explanation: 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]. - 解题思路
题意是将两个字符串删除个别字符后得到相同的字符串,求得删除字符的ASCII码的值的最小和,那么反向思考也是求得剩下字符的ASCII码的和的最大值,因为字符串中字符的总的ASCII码值的总和是不变的。下面就可以将问题转化为:求得两个字符串中公共子序列的最大的ASCII码值的和,对求最长公共子序列的方法稍加改变就可得到该问题的答案。
求解最长子序列的状态转移方程为:用l[i][j]记录以s1[i]和s2[j]结尾的最长子序列的长度if s1[i] == s2[j]l[i][j] = l[i-1][j-1] + 1;else l[i][j] = max(l[i-1][j], l[i][j-1]);==========================================================求解该问题的状态转移方程为:用dp[i][j]记录以s1[i]和s2[j]结尾的子序列的最大的ASCII码值的总和if s1[i] == s2[j]dp[i][j] = dp[i-1][j-1] + (int)s1[i]; elsedp[i][j] = max(dp[i-1][j], dp[i][j-1]);
- 代码如下
class Solution {public: int minimumDeleteSum(string s1, string s2) { int len1 = s1.length(); int len2 = s2.length(); int dp[len1+1][len2+1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; 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]); } } } int all = 0; for(int i = 0; i < len1; i ++) { all += (int)s1[i]; } for(int j = 0; j < len2; j ++) { all += (int)s2[j]; } return all - 2*dp[len1][len2]; }};
原题地址
如有错误请指出,谢谢!
阅读全文
0 0
- Leetcode 712. Minimum ASCII Delete Sum for Two Strings
- [leetcode] 712. Minimum ASCII Delete Sum for Two Strings
- Leetcode: 712. Minimum ASCII Delete Sum for Two Strings
- LeetCode 712. Minimum ASCII Delete Sum for Two Strings [Medium]
- [LeetCode]712. Minimum ASCII Delete Sum for Two Strings
- [LeetCode] DP之 Minimum ASCII Delete Sum for two Strings
- LeetCode#712 Minimum ASCII Delete Sum for Two Strings (week10)
- LeetCode #712 Minimum ASCII Delete Sum for Two Strings
- Leetcode Minimum ASCII Delete Sum for Two Strings
- Leetcode-Minimum ASCII Delete Sum for Two Strings
- leetcode 712 Minimum ASCII Delete Sum for Two Strings
- Leetcode 712 Minimum ASCII Delete Sum for Two Strings
- LeetCode:Minimum ASCII Delete Sum for Two Strings
- leetcode 712 Minimum ASCII Delete Sum for Two Strings
- Minimum ASCII Delete Sum for Two Strings
- Minimum ASCII Delete Sum for Two Strings
- 712. Minimum ASCII Delete Sum for Two Strings
- LWC 55:712. Minimum ASCII Delete Sum for Two Strings
- ubuntu 下使用 upnp 配置路由器端口映射
- org.apache.catalina.core.StandardWrapperValve invoke的解决办法
- usaco6.4.2 Electric Fences
- 德语词汇笔记(一)
- 微服务注册与发现及如何使用Eureka
- [LeetCode]712. Minimum ASCII Delete Sum for Two Strings
- 机器学习实战篇-人脸识别(1)- 人脸定位
- ICMP协议
- nagios插件之检测tomcat日志的startAt值(待改进)
- 趣图:程序员调 Bug 的 5 个阶段
- 数据库的完整性
- 老说程序员如何看产品经理,今天说说产品经理讨厌哪些程序员
- ARPA网
- 高级程序设计——高级定时器