Returns the maximum number of identical substrings in two strings.

Source: Internet
Author: User
2. Obtain the largest and identical substrings in two strings. The first action: print the short string with a child string whose length is reduced once.
"Cvhellobnmtanop"
"Andefc"
Ideas:
1. Obtain the short substring in descending order of length.
2. Determine whether to include each obtained substring in the long string. If it contains, It is found!


Package Tan; Class test {public static string getmaxsubstring (string S1, string S2) {string max = "", min = ""; // obtain the large substring max = (s1.length ()> s2.length ())? S1: S2; // obtain the small substring min = (max = S1 )? S2: S1; // Sop ("max =" + MAX + "... min = "+ min); // the outer loop traverses the entire small substring for (INT x = 0; x <min. length (); X ++) {// What is the traversal of the inner loop? For (INT y = 0, Z = min. Length ()-X; Z! = Min. length () + 1; y ++, Z ++) {string temp = min. substring (Y, Z); Sop (temp); If (max. contains (temp) // If (s1.indexof (temp )! =-1) return temp;} return "";} public static void main (string [] ARGs) {string S1 = "AB"; string S2 = "cvhellobnm "; sop (getmaxsubstring (S2, S1);} public static void Sop (string Str) {system. out. println (STR );}}


The program output result is:

Andefc
Andef
Ndefc
Ande
Ndef
Defc
And
NDE
Def
EFC
An
An


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.