"Java" to find the longest common substring of two strings

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.