第一行输入一个长度为
、仅由小写字母组成的字符串
。
第二行输入一个长度为
、仅由小写字母组成的字符串
。
输出一个整数,代表
和
的最长公共子串的长度。
awaabb aawbb
2
在这个样例中,
和
都是
和
的最长公共子串。
asdfas werasdfaswer
6
动态规划题目,dp[i][j]含义为以i - 1为结尾的str1和以j - 1为结尾的str2的最长公共子串,这样可以避免判断第一个串的第一个字符和第二个串的字符是否相等。当str1.charAt(i - 1)和str2.charAt(j - 1)相等时,dp[i][j]和dp[i - 1][j - 1] + 1取最大值。随时遍历dp[i][j]的最值,最后返回给控制台即可。7.17
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str1 = in.nextLine();
String str2 = in.nextLine();
int[][] dp = new int[str1.length() + 1][str2.length() + 1];
int max = 0;
for (int i = 1; i <= str1.length(); i++){
for (int j = 1; j <= str2.length(); j++){
if (str1.charAt(i - 1) == str2.charAt(j - 1)){
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
max = Math.max(max, dp[i][j]);
}
}
}
System.out.println(max);
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String stra = sc.next();
String strb = sc.next();
int a = stra.length();
int b = strb.length();
int maxLength = 0;//定义初始公共子串长度为0
int shorterLen = a < b ? a : b;//定义较短字符串的长度
for (int i = 1; i <= shorterLen; i++) {
//不区分长短字符串,直接遍历a字符串的所有子串与b比较
// (限定了较短字符串长度shorterLen,不怕超出范围)
for (int j = 0; j <= a - i; j++) {
//i是子串长度, j是子串起始位置
String str = stra.substring(j, j + i);
//截取尾索引为stra的长度,因此j+i<=a,j<=a-i
if (strb.contains(str)) {
maxLength++;
//找到一次即跳出循环,继续找下一个子串
break;
}
}
}
System.out.println(maxLength);
}
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
System.out.println(getMaxSubString(a, b));
}
}
private static int getMaxSubString(String a, String b) {
int len1 = a.length();
int len2 = b.length();
int[][] dp = new int[len1 + 1][len2 + 1];
int max = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
max = Math.max(max, dp[i][j]);
} else {
dp[i][j] = 0;
}
}
}
return max;
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
String str2 = in.nextLine();
int maxLength = 0;
for (int i = 0; i < str1.length(); i++) {
for (int j = i; j < str1.length(); j++) {
String substring = str1.substring(i, j + 1);//j + 1需要包含最后一位
if (str2.contains(substring)) {
maxLength = Math.max(maxLength, substring.length());
}
}
}
System.out.println(maxLength);
in.close();
}
} import java.util.*;
//动态规划
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int len1 = str1.length();
int len2 = str2.length();
//状态方程dp[i][j]:字符串1长度为i,字符串2长度为j时最长公共字串长度
int[][] dp = new int[len1 + 1][len2 + 1];
int max = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
//动规表达式,本组相等取前一组相等的长度加1
dp[i][j] = dp[i - 1][j - 1] + 1;
max = Math.max(max, dp[i][j]);
}
}
}
System.out.println(max);
}
} import java.util.Scanner;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
String str2 = br.readLine();
if(str1.length()>str2.length()){
String str =str1;
str1 = str2;
str1 = str;
}
if(str2.contains(str1)){
System.out.println(str1.length());
return;
}
for(int i = 1;i<str1.length();i++){
for(int j = 0;j<=i;j++){
String str;
str = str1.substring(j,str1.length()-(i-j));
if(str2.contains(str)){
System.out.println(str.length());
return;
}
}
}
System.out.println(0);
}
} 19ms,求大佬分析为啥比榜上的慢这么多
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line1 = br.readLine();
String line2 = br.readLine();
// 找出短的字符串
String shortStr = line1.length() <= line2.length() ? line1 : line2;
String longStr = line1.length() <= line2.length() ? line2 : line1;
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < shortStr.length(); i++) {
for (int j = i + 1; j <= shortStr.length(); j++) {
String sub = shortStr.substring(i, j);
if (longStr.contains(sub)) {
map.put(sub, sub.length());
}
}
}
// 计算最大长度
if (map.size() > 0) {
Collection<Integer> values = map.values();
int max = Collections.max(values);
System.out.println(max);
} else {
System.out.println(0);
}
}
} import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String str1=br.readLine();
String str2=br.readLine();
int len1=str1.length();
int len2=str2.length();
//记录str1的第i字符和str2的第j字符的最长公共子串长度
int[][] dp = new int[len1+1][len2+1];
//maxLCS记录最终的最长公共子串长度
int maxLCS=0;
for(int i=1;i<=len1;i++){
for(int j=1;j<=len2;j++){
//str1.charAt(i-1)==str2.charAt(j-1)时,最长公共子串长度dp[i][j]等于没有str1的第i
//字符和str2的第j字符参与比较时的最长公共子串长度+1
if(str1.charAt(i-1)==str2.charAt(j-1)) dp[i][j]=dp[i-1][j-1]+1;
if(dp[i][j]>maxLCS) maxLCS=dp[i][j];
}
}
System.out.println(maxLCS);
}
}