Java implementation string matching to find the largest common substring of two strings _java

Source: Internet
Author: User

This article illustrates the Java implementation method for finding the largest common substring of two strings. Share to everyone for your reference, specific as follows:

Recently, there is a need for text comparison in the project work, after this period of study, summed up this blog content: To find two strings of the largest common substring.

algorithm idea: to compute a common substring of two strings based on graphs. The concrete algorithm thought refers to the following figure:

Input string S1:ACHMACMH input string S2:macham

    1. Step A is to split the string s1,s2 into a two-dimensional array, separated by byte;
    2. The values in a two-dimensional array are shown in B, for example, the first row of the first column indicates whether the first byte of the string S2 and S1 is equal, if equal is 1, otherwise it is 0, resulting in the two-dimensional array shown in B;
    3. The common factor on a slash in a two-dimensional array (slash is the lower-right corner of element A, that is, the next element of A[i][j] is a[i+1][j+1]; The common factor is a string composed of the position of 1);
    4. Sorts all common factors, returning the value of the largest common factor.

The specific implementation code looks like this:

Package cn.lulei.compare; 
Import java.util.ArrayList; 
Import java.util.Collections; 
Import Java.util.Comparator; 
 
Import java.util.List; 
  public class Stringcompare {private int A; 
   
  private int B; 
    public string getmaxlengthcommonstring (string s1, string s2) {if (S1 = = NULL | | s2 = NULL) {return null; 
    A = S1.length ();//s1 length do row B = s2.length ();//s2 length make column if (a== 0 | | b = = 0) {return ""; 
    //Set the matching matrix Boolean [] array = new BOOLEAN[A][B]; 
      for (int i = 0; i < A; i++) {Char C1 = S1.charat (i); 
        for (int j = 0; J < b; j +) {char C2 = S2.charat (j); 
        if (c1 = = C2) {Array[i][j] = true; 
        else {Array[i][j] = false; All the common factor strings, save the information as the starting position and length relative to the second string list<childstring> childstrings = new Arraylist<chil 
    Dstring> (); 
    for (int i = 0; i < A; i++) {getmaxsort (i, 0, array, childstrings); } 
    for (int i = 1; i < b i++) {getmaxsort (0, I, array, childstrings); 
    }//sorted sort (childstrings); 
    if (Childstrings.size () < 1) {return ""; 
    }//Returns the maximum common factor string int max = Childstrings.get (0). MaxLength; 
    StringBuffer sb = new StringBuffer (); 
      for (childstring s:childstrings) {if (max!= s.maxlength) {break; 
      } sb.append (S2.substring (S.maxstart, S.maxstart + s.maxlength)); 
    Sb.append ("\ n"); 
  return sb.tostring (); }//Sort, flashback private void sort (list<childstring> list) {Collections.sort (list, new Comparator<childs 
      Tring> () {public int compare (childstring O1, childstring O2) {return o2.maxlength-o1.maxlength; 
  } 
    }); 
    }//Find the common factor string on a slash private void getmaxsort (int i, int J, Boolean [] array, list<childstring> Sortbean) { 
    int length = 0; 
    int start = j; for (; I < a && J < b; i++,j++) {if (Array[i][j]) {length++; 
        else {sortbean.add (new childstring (length, start)); 
        length = 0; 
      Start = j + 1; 
      } if (i = = A-1 | | j = = b-1) {sortbean.add (new childstring (length, start)); 
    }}//Common factor class childstring {int maxLength; 
     
    int Maxstart; 
      childstring (int maxLength, int maxstart) {this.maxlength = MaxLength; 
    This.maxstart = Maxstart; }/** * @param args */public static void main (string[] args) {//TODO auto-generated Method St 
  UB System.out.println (New Stringcompare (). getmaxlengthcommonstring ("ACHMACMH", "Macham")); 
 } 
}

The result of the program's final execution is:

For the two files of the comparison to the individual think that can refer to this algorithm idea (oneself now and for implementation), in future blog will be written.

In the above implementation process, using an array to save all of the public substring information, and then sort the largest substring, this approach if only the maximum substring, the algorithm is not very reasonable, so do the following modifications, list only save the current calculation of the largest substring, specifically implemented as follows:

/** * @Description: string comparison * * Package com.lulei.test; 
Import java.util.ArrayList; 
 
Import java.util.List; 
  public class Stringcompare {private int A; 
  private int B; 
   
  private int maxLength =-1; 
    public string getmaxlengthcommonstring (string s1, string s2) {if (S1 = = NULL | | s2 = NULL) {return null; 
    A = S1.length ();//s1 length do row B = s2.length ();//s2 length make column if (a== 0 | | b = = 0) {return ""; 
    //Set the matching matrix Boolean [] array = new BOOLEAN[A][B]; 
      for (int i = 0; i < A; i++) {Char C1 = S1.charat (i); 
        for (int j = 0; J < b; j +) {char C2 = S2.charat (j); 
        if (c1 = = C2) {Array[i][j] = true; 
        else {Array[i][j] = false; All the common factor strings, save the information as the starting position and length relative to the second string list<childstring> childstrings = new Arraylist<chil 
    Dstring> (); 
 for (int i = 0; i < A; i++) {getmaxsort (i, 0, array, childstrings);   for (int i = 1; i < b i++) {getmaxsort (0, I, array, childstrings); 
    StringBuffer sb = new StringBuffer (); 
      for (childstring s:childstrings) {sb.append (s2.substring (S.maxstart, S.maxstart + s.maxlength)); 
    Sb.append ("\ n"); 
  return sb.tostring (); 
    }//Find the common factor string on a slash private void getmaxsort (int i, int J, Boolean [] array, list<childstring> Sortbean) { 
    int length = 0; 
    int start = j; 
      for (; I < a && J < b; i++,j++) {if (Array[i][j]) {length++; 
        else {//Direct add, save all substrings below, save only the current maximum substring//sortbean.add (new childstring (length, start)); 
        if (length = = maxLength) {sortbean.add (new childstring (length, start)); 
          else if (length > MaxLength) {sortbean.clear (); 
          maxLength = length; 
        Sortbean.add (new childstring (length, start)); 
        length = 0; 
  Start = j + 1;    } if (i = = A-1 | | j = = b-1) {//Direct add, save all substrings, the following judgment, save only the current largest substring//sortbean.add (new childstring ( 
        Length, start)); 
        if (length = = maxLength) {sortbean.add (new childstring (length, start)); 
          else if (length > MaxLength) {sortbean.clear (); 
          maxLength = length; 
        Sortbean.add (new childstring (length, start)); 
    }}///Common factor class childstring {int maxLength; 
     
    int Maxstart; 
      childstring (int maxLength, int maxstart) {this.maxlength = MaxLength; 
    This.maxstart = Maxstart; }/** * @param args */public static void main (string[] args) {//TODO auto-generated Method St 
  UB System.out.println (New Stringcompare (). getmaxlengthcommonstring ("abcdef", "DEFABC")); 
 } 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.