当前位置:网站首页>最长公共子串
最长公共子串
2022-08-02 03:33:00 【小艾菜菜菜】
算法——最长公共子串(一张图片看懂)
概述
给定str1与str2两个字符串,找出其中最长的公共子串。
思路
用str1和str2建立一个二维矩阵,很轻易的就能从中发现关系。矩阵的大小为:
int[][] dp = new int[str1.lenth()+1][str2.length()+1]
- 1
这是为了方便substring的操作而已。
下面给出图示方便理解:
代码
public class 最长公共子串 {
/** * longest common substring * @param str1 string字符串 the string * @param str2 string字符串 the string * @return string字符串 * 将str1与str2组成一个二维矩阵,相同的地方标记为1,那么可以观察到对角线处相连的1就是一个连续的公共子串; * 为了搞到最大的值,则只需要将对角线的1不断地累加并保存即可,待矩阵构建完毕,我们就已经知道最大的子串长为多少, * 以及在哪个index结束。 */
public String LCS (String str1, String str2) {
//异常处理
if (str1 == null || str2 == null)
return str1 == null ? str2 : str1;
//动态规划
int len1 = str1.length(),
len2 = str2.length(),
max = 0,
index = 0;
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i < len1; i++){
for (int j = 0; j < len2; j++){
//如果字符相等,则这个子串的长度取决于之前的子串长度 + 1
if (str1.charAt(i) == str2.charAt(j)){
dp[i + 1][j + 1] = dp[i][j] + 1;
}
if (max < dp[i + 1][j + 1]){
max = dp[i + 1][j + 1];
index = i + 1;
}
}
}
//截取字符串并返回
return max == 0 ? "-1" : str1.substring((index - max),(index));
}
}
边栏推荐
- ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
- IoT solution
- 电子密码锁_毕设‘指导’
- 【nRF24L01 connects with Arduino to realize wireless communication】
- VCA821可变增益放大器
- list:list的介绍和模拟实现
- MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
- Comparative analysis of OneNET Studio and IoT Studio
- 引擎开发日志:集成Bullet3物理引擎
- idea中创建jsp项目详细步骤
猜你喜欢
随机推荐
【MQ-3 Alcohol Detector and Arduino Detect Alcohol】
剑指Offer 32.Ⅱ从上到下打印二叉树
path 修补文件命令
TeamCode 产品 UI 全新升级,快来体验吧
读取FBX文件踩坑清单
MQ-5 combustible gas sensor interface with Arduino
list:list的介绍和模拟实现
STM32 CAN 介绍以及相关配置
【plang 1.4.6】Plang高级编程语言(发布)
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
字符串匹配(蛮力法+KMP)
Comparison between Boda Industrial Cloud and Alibaba Cloud
WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?
【LeetCode】合并
字符串哈希
Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development
【面试必看】链表的常见笔试题
使用pyqt弹出消息提示框
哈希表解题方法
向龙芯2K1000板子上烧写中标麒麟系统









