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: 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 strings are 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].

两个字符串最长子序列。

Runtime: 12 ms, faster than 76.47% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.

#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int dp[s1.size()+][s2.size()+];
memset(dp, , sizeof(dp));
int sum = ;
for(int i=; i<s1.size(); i++){
sum += (int)s1[i];
for(int j=; j<s2.size(); j++){
if(s1[i] == s2[j]) {
dp[i+][j+] = dp[i][j] + (int)s1[i];
}else {
dp[i+][j+] = max(dp[i+][j], dp[i][j+]);
}
}
}
for(int i=; i<s2.size(); i++){
sum += (int)s2[i];
}
return sum - *dp[s1.size()][s2.size()];
}
};

最新文章

  1. Handler系列之使用
  2. Cocoapods - pod install 成功后找不到头文件解决
  3. pwnable echo1
  4. 【android studio】android studio使用过程中,搜集的一些问题
  5. django book querysets
  6. zigbee学习之路(七):定时器3(中断方式)
  7. JS 信息提示弹框封装
  8. CRM 2013 系统设置新功能二:Entity images 图像字段
  9. ZOJ3471--Most Powerful(状压DP)
  10. pageContext.request.contextPath
  11. Linux 循环设备 /dev/loop 解惑
  12. How To Cluster Rabbit-MQ--reference
  13. C#获取时间属于第几周
  14. 如何解决svn图标不显示呢?
  15. Java Properties类源码分析
  16. JSTL的相关使用
  17. 从零开始学习PYTHON3讲义(五)while循环和棋盘麦粒问题
  18. 采用synchronized关键字写一个显示锁
  19. BZOJ 5097: [Lydsy1711月赛]实时导航(最短路 + bitset)
  20. 廖雪峰Java6 IO编程-2input和output-7序列化

热门文章

  1. qt tableview中如何添加右键菜单且不可编辑单元格
  2. 6、SSH远程管理服务实战
  3. Cowrie蜜罐部署教程
  4. 论文笔记:Unsupervised Domain Adaptation by Backpropagation
  5. Hadoop_07_HDFS的Java API 操作
  6. Vue快速学习_第五节
  7. 浅入深出Vue:文章编辑
  8. Java常用类(一)Math类和Random类
  9. loj2424 「NOIP2015」子串[字符串DP]
  10. 1024&#183;程序员节来啦!MyEclipse致敬改变世界的程序猿