The oldest and oldest of the Java series

Source: Internet
Author: User

    • Oldest sequence: Matched characters do not need to be contiguous.
    • Oldest string: Matched characters need to be contiguous and may have multiple results.

workaround : Treat input string 1 as a row, input string 2 as a column, form a two-bit array, and then mark the value of the diagonal match character as 1, and calculate the number of matching characters that meet the criteria.

Basic idea: Space change time, dynamic planning.

Plots and formulas (similar to the oldest-oldest sequence, the oldest string)

State transition equation

Intuitive version:

Oldest-oldest sequence

1     /**2 * Find longest common sequence from the input string3      * @paramS14      * @paramS25      * @returnlength of longest common sequence6      */7      Public Static intLCS (string s1, string s2) {8         int[] C =New int[S1.length ()][s2.length ()];9 Ten         //initialize the elements without top left element One          for(inti=0; I<s1.length (); i++){ A             if(S1.charat (i) = = S2.charat (0)) { -C[i][0] = 1; -             } the         } -          for(intj = 0; J<s2.length (); j + +){ -             if(S1.charat (0) = =S2.charat (j)) { -C[0][J] = 1; +             } -         } +          for(inti = 1; I < s1.length (); i++) { A              for(intj = 1; J < S2.length (); J + +) { at                 if(S1.charat (i) = =S2.charat (j)) { -C[I][J] = c[i-1][j-1] + 1; -}Else if(C[i][j-1] > C[i-1][j]) { -C[I][J] = c[i][j-1]; -}Else { -C[I][J] = c[i-1][j]; in                 } -             } to         } +         returnC[s1.length ()-1][s2.length ()-1]; -}

The oldest sequence can also be sorted and then matched with a stable sorting algorithm. such as using the merge sorting algorithm (note that the fast sort is unstable).

The eldest son of a string

1     /**2 * Find longest substring from the input string3      *4      * @paramS15      * @paramS26      * @returnlength of longest substring7      */8      Public Static intLSS (string s1, string s2) {9         int[] C =New int[S1.length ()][s2.length ()];Ten         intMax = 0; One  A         //initialize the elements without top left element -          for(inti=0; I<s1.length (); i++){ -             if(S1.charat (i) = = S2.charat (0)) { theC[i][0] = 1; -             } -         } -          for(intj = 0; J<s2.length (); j + +){ +             if(S1.charat (0) = =S2.charat (j)) { -C[0][J] = 1; +             } A         } at  -          for(inti = 1; I < s1.length (); i++) { -              for(intj = 1; J < S2.length (); J + +) { -                 if(S1.charat (i) = =S2.charat (j)) { -C[I][J] = c[i-1][j-1] + 1; -                     if(C[i][j] >max) { inMax =C[i][j]; -                     } to                 } +             } -         } the         returnMax; *}

Optimized version:

Cond..

Optimize the basic idea:

You can use recursion to discard non-conforming matches as early as possible.

For the optimization of the oldest string,

You can first find the oldest string, and if a match is found, keep looking for it, and mark the final mismatch as-1 instead of 0.

If the remaining possible match length is less than the length found, stop the recursive operation and return directly.

Other related algorithms

Horspool ' s algorithm and Boyer-moore algorithm

The oldest and oldest of the Java series

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.