This article illustrates the Java implementation method for finding the longest common substring of two strings. Share to everyone for your reference, specific as follows:
This is a topic on Huawei's OJ. First of all, if we write code in Java, Huawei OJ has the following three rules to comply with, otherwise the compilation cannot pass or use case cannot pass, the rules are as follows:
(1) There must be no package name;
(2) The main class name can only be main;
(3) It is not possible to output information unrelated to the result.
Well, according to the rules above, the code we write is as follows (this code is not optimal, but is used to record the writing rules of the Java code on Huawei OJ):
Import Java.util.Scanner;
public class Main {public static void main (string[] args) {Scanner sc = new Scanner (system.in);
Main mainobj = new Main ();
int len = mainobj.getcommonstrlength (Sc.next (), Sc.next ());
System.out.println (len);
int Getcommonstrlength (String str1, String str2) {str1 = Str1.tolowercase ();
STR2 = Str2.tolowercase ();
int len1 = Str1.length ();
int len2 = Str2.length ();
String min = null;
String max = null;
String target = null; min = len1 <= len2?
STR1:STR2; max = len1 > len2?
STR1:STR2; Outermost: The length of the min substring, starting with the maximum length for (int i = Min.length (); I >= 1; i--) {//traversing the min substring of length I, starting with 0 for (int j = 0; J <= Min.length ()-I;
J + +) {target = Min.substring (J, j + i); Traverse the max substring of length I to determine whether it is the same as the target substring, starting with 0 for (int k = 0; k <= max.length ()-i; k++) {if (max.substring k,k + i
). Equals (target)) {return i;
return 0; }
}
I hope this article will help you with your Java programming.