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