This is a topic on Huawei OJ. First, if we write code in Java, Huawei OJ has the following three rules to follow, otherwise the compilation will not pass or the use case cannot pass, the rule is as follows:
(1) must not have the package name, (2) The main class name can only be main, (3) can not output information unrelated to the result.
Well, in accordance with the above rules, we write the code as follows (this code is not optimal, just to record the Java code on the Huawei OJ Rules of writing):
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); Main Mainobj=NewMain (); intLen =mainobj.getcommonstrlength (Sc.next (), Sc.next ()); System.out.println (len); } intgetcommonstrlength (String str1, String str2) {str1=str1.tolowercase (); STR2=str2.tolowercase (); intLen1 =str1.length (); intLen2 =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 from the maximum length for(inti = Min.length (); I >= 1; i--) { //traverse the min substring of length I, starting from 0 for(intj = 0; J <= Min.length ()-I; J + +) {target= Min.substring (J, J +i); //traverse the max substring of length I to determine if it is the same as the target substring, starting with 0 for(intk = 0; K <= Max.length ()-I; k++) { if(Max.substring (K,k +i). Equals (target) {returni; } } } } return0; }}
"Java" to find the longest common substring of two strings