当前位置:网站首页>Daily question series: common substring calculation
Daily question series: common substring calculation
2022-04-22 12:49:00 【Gao You Wu Shao】
link :https://www.nowcoder.com/questionTerminal/98dc82c094e043ccb7e0570e5342dd1b
source : Cattle from
Given two strings that contain only lowercase letters , Calculate the length of the largest common substring of two strings .
notes : The definition of substring means that a string deletes some of its prefixes and suffixes ( It's also possible to delete ) String formed after .
Data range : String length :1≤s≤150
Advanced : Time complexity :O(n^3) , Spatial complexity :O(n)

Their thinking :
For dynamic programming , We can't solve this big problem all at once , It needs to be divided into small problems , Let's first look at the length of the maximum common substring between the substrings of each string , Then use the solution of the small problem to push down the big problem
Let's define a function :
F(i,j) Represents the first string i The end of the first character and the second string j The length of the largest common substring ending in characters
for instance :

F(1,1) Express : The of the first string starts with 1 Characters a The ending substring , The of the second string starts with 1 Characters w The ending substring , The maximum common substring length is 0
In the same way :
F(2,3): … s,… r, The maximum common substring length is 0
F(4,7): …f,… f, The maximum common substring length must be greater than or equal to 1 了 (f It's the same ,f The previous one may be the same )
Spread it :
If the first i Characters != The first j Characters
F(i,j)=0
The first i Characters == The first j Characters
F(i,j)=F(i-1,j-1)+1
such as F(1,4)=1 character string a
F(2,5)=F(1,4)+1=2 character string as
F(3,6)=F(2,5)+1=3 character string asd
The initial state :F(i,0)=F(0,j)=0
( You compare a string to an empty string , Their substring must be 0)
Return value :max(F(i,j))
The code is as follows :
import java.util.*;
public class Main{
public static int getMaxLen(String str1,String str2){
char[] arr1=str1.toCharArray();
char[] arr2=str2.toCharArray();
int l1=arr1.length;
int l2=arr2.length;
int[][] maxSubLen=new int[l1+1][l2+1];
int maxLen=0;
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(arr1[i-1]==arr2[j-1]){
//F(i,j)=F(i-1,j-1)+1
maxSubLen[i][j]=maxSubLen[i-1][j-1]+1;
// Whether the maximum value needs to be updated
if(maxLen<maxSubLen[i][j]){
maxLen=maxSubLen[i][j];
}
}// If the range , You don't have to worry about it , The default in the original array is 0
}
}
return maxLen;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str1=scanner.nextLine();
String str2=scanner.nextLine();
int x=getMaxLen(str1,str2);
System.out.println(x);
}
}
版权声明
本文为[Gao You Wu Shao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221247059767.html
边栏推荐
- R language data Table import data practice: data It is more convenient to set key, compound key, delete key and data join after setting key value. After setting key value, you can use keyby syntax ins
- 迪赛智慧数——折线图(动态折线图):近1年汽柴油调价情况
- 诺瓦星云更新招股书,继续上市进程,“学术派”的步步为盈?
- Practice of importing SAS files with R language
- Share some practical skills accumulated in a recent mobile terminal project
- let和var的区别面试题答案
- 可执行文件的生成过程
- Is this open source project going to kill me?
- Today's sleep quality record 76 points
- 制裁压力下,俄罗斯计划通过友好国家平行进口电子产品
猜你喜欢

Book city project registration page and email verification

書城項目注册頁面和郵箱驗證

Generation process of executable file

Read attention concatenation volume for accurate and efficient stereo matching

分享一下自己最近写项目遇到的小问题

51单片机案例(1)——利用DS1302实现实时时钟和可调时钟的功能

. net treasure API: outputformatter, format output object

Is this open source project going to kill me?

【生活杂谈】中体平台教你如何提高彩票中奖率

Ros2 learning notes (VII) tools for learning ros2 from turnlesim
随机推荐
The difference between let and VaR, several classic small questions
Get rid of the "small workshop" of AI production: how to build a cloud native AI platform based on kubernetes
Share the small problems you have encountered in writing projects recently
Express Can‘t set headers after they are sent. problem
让cpolar获得固定TCP地址
H5 < canvas > label + native JS self-made a simple drawing tool
Machine learning training template, summarizing multiple classifiers
JS基础11
JS输出随机数
51 single chip microcomputer case (1) -- using DS1302 to realize the functions of real-time clock and adjustable clock
Share some practical skills accumulated in a recent mobile terminal project
JS基础10
Use R language_ Xlsx function export and write dataframe data to excel file
Chinese characters and screenshots suitable for the test boundary are selected
ROS2学习笔记(六)从turtlesim学习ROS2动作
Simple deployment of microservices
Generation process of executable file
线程相关问题
嵌入式开发:在嵌入式系统中验证传感器和通信数据的3个技巧
男女配对问题