【每日一题】【动态规划】2022年1月30日-NC127 最长公共子串
描述

方法1:dp数组存子串
import java.util.*; public class Solution {
/**
* longest common substring
* @param str1 string字符串 the string
* @param str2 string字符串 the string
* @return string字符串
*/
public String LCS (String str1, String str2) {
int len1 = str1.length(), len2 = str2.length();
String[][] dp = new String[len1 + 1][len2 + 1]; //为何初始化为len+1,相当于从1-n
int max = 0;
String res = "";
for(int i = 0; i <= len1; i++) {
for(int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
dp[i][j] = "";
} else if(str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + str1.charAt(i - 1);
if(dp[i][j].length() > max) {
max = dp[i][j].length();
res = dp[i][j];
}
} else {
dp[i][j] = "";
}
}
}
return res;
}
}
类似题目:NC92 最长公共子序列(二)
方法2:dp数组存公共串长度
public static String LCS (String str1, String str2) {
int len1 = str1.length(), len2 = str2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
int max = 0;
int index = 0;
for(int i = 0; i < len1; i++) {
for(int j = 0; j < len2; j++) {
if(str1.charAt(i) == str2.charAt(j)) {
dp[i + 1][j + 1] = dp[i][j] + 1;
}
if(dp[i + 1][j + 1] > max) {
max = dp[i + 1][j + 1];
index = i;
}
}
}
return str1.substring(index - max + 1, index + 1);
}
【每日一题】【动态规划】2022年1月30日-NC127 最长公共子串的更多相关文章
- 2022年5月11日,NBMiner发布了41.3版本,在内核中加入了100%LHR解锁器,从此NVIDIA的显卡再无锁卡一说
2022年5月11日,NBMiner发布NBMiner_41.3版本,主要提升了稳定性. 2022年5月8日,NBMiner发布NBMiner_41.0版本,在最新的内核 ...
- 关于2022年3月9日之后Typora登录不了--已解决
p.s.今天是2022.7.27,软件版本:13.6.1 (以下所有方法,亲自尝试后整理出的) 报错信息: This beta version of typora is expired, please ...
- 北京Uber优步司机奖励政策(1月30日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(12月30日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 2016年12月30日 星期五 --出埃及记 Exodus 21:25
2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...
- 11月30日《奥威Power-BI智能分析报表制作方法》腾讯课堂开课啦
这么快一周就过去了,奥威公开课又要与大家见面咯,上节课老师教的三种报表集成方法你们都掌握了吗?大家都知道,学习的结果在于实际应用,想要熟练掌握新内容的要点就在于去应用它.正是基于这一要点,每一期的课程 ...
- 动态规划(一)——最长公共子序列和最长公共子串
注: 最长公共子序列采用动态规划解决,由于子问题重叠,故采用数组缓存结果,保存最佳取值方向.输出结果时,则自顶向下建立二叉树,自底向上输出,则这过程中没有分叉路,结果唯一. 最长公共子串采用参考串方式 ...
- 2016年11月30日 星期三 --出埃及记 Exodus 20:21
2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...
- 2016年10月30日 星期日 --出埃及记 Exodus 19:15
2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...
- 2016年6月30日 星期四 --出埃及记 Exodus 14:27
2016年6月30日 星期四 --出埃及记 Exodus 14:27 Moses stretched out his hand over the sea, and at daybreak the se ...
随机推荐
- ProxySQL Main库
转载自:https://www.jianshu.com/p/eee03c5ec879 Main库表清单 Admin> SHOW TABLES FROM main; +-------------- ...
- Linux日志切割方法[Logrotate、python、shell实现方式]
Linux日志切割方法[Logrotate.python.shell实现方式] 对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分 ...
- 18. Fluentd输出插件:out_stdout用法详解
stdout即标准输出,out_stdout将收到的日志事件打印到标准输出. 如果Fluentd以daemon方式在后台运行,out_stdout会将事件输出到Fluentd的运行日志中. 这个插件在 ...
- Maven+SpringMVC+Dubbo 简单的入门demo配置
转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...
- 51单片机下实现软件模拟IIC通信
1.IIC协议简易概述 IIC全称Inter-Integrated Circuit (集成电路总线),是由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备.IIC属于半双 ...
- vue3 vite2 封装 SVG 图标组件 - 基于 vite 创建 vue3 全家桶项目续篇
在<基于 vite 创建 vue3 全家桶>一文整合了 Element Plus,并将 Element Plus 中提供的图标进行全局注册,这样可以很方便的延续 Element UI 的风 ...
- 详解平衡二叉树(AVL tree)平衡操作(图+代码)
* 左左就右旋,右右就左旋 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int max ...
- CentOS 7.9 安装 ELK
一.CentOS 7.9 安装 elasticsearch-7.8.1 地址 https://www.elastic.co https://www.elastic.co/cn/downloads/pa ...
- prometheus监控实战
第一节.环境和软件版本 1.1.操作系统环境 主机ip 操作系统 部署软件 备注 192.168.10.10 Centos7.9 Grafana.Pushgateway.Blackbox Export ...
- 华为设备配置和使用FTP服务命令
配置SFTP Server与Client server:aaa 进入aaa视图 local-user huawei2 password cipher huawei2 设置用户名和密码 local-us ...