Java implementation method for finding the longest common substring of two strings _java

Source: Internet
Author: User

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.

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.