Dark Horse Programmer _ Diary 25_java the maximum identical substring of two strings

Source: Internet
Author: User

——-Android Training, Java training, look forward to communicating with you! ———-

/* Gets the maximum of the same substring in two strings. First action: Prints the short string with a reduced length of substring."Abcwerthelloyuiodef"    "CVHELLOBNM"Simulate: First trip: Max substring: cvhellobnm↑--------↑Find in a long stringAbcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Abcwerthelloyuiodef↑--------↑Not found. Second trip: Maximum substring1: cvhellobnm↑-------↑Maximum substring2: cvhellobnm↑-------↑Continue to match in long strings if not found. Third trip: Maximum substring1: cvhellobnm↑------↑Maximum substring2: cvhellobnm↑------↑Maximum substring3: cvhellobnm↑------↑Continue to match in long strings if not found. IV Trip: Maximum substring1: cvhellobnm↑-----↑Maximum substring2: cvhellobnm↑-----↑Maximum substring3: cvhellobnm↑-----↑Maximum substring4: cvhellobnm↑-----↑······ Stops if found, returns the maximum same substring, if the maximum substring is"", the maximum same substring is not put back. According to the above simulation process, the following ideas can be obtained: The first step: Select the short length of the string in two strings as key, the long one is str; go to the second step. Second step: From left to right, match key in Str, if successful, go to fourth step; otherwise, go to the third step. Step three: Reduce the key length by one and get multiple substrings of the same length, each of which performs the second step in turn. When key is an empty string, go to step fifth. Fourth step: Match success, end match. Fifth step: Match failed. Reviews: The third step is not analyzed clearly, so did not write it out.Key. Length ()-0Sub-string1AKey. Length ()-1Sub-string1+1AKey. Length ()-2Sub-string1+1+1AKey. Length ()-3Sub-string1+1+1+1Can be observed, the third step with cycle nested small loop implementation. Cycle as key */classGetmaxsubstringdemo {public static void main(String[] args){Stringstr1 = "Abcwerthelloyuiodef";Stringstr2 = "CVHELLOBNM";System. out.println( getmaxsubstring(str1,str2)); }//Match maximum same substring public staticStringGetmaxsubstring(string str1,string str2){//Compare two string lengths, long as STR, short as keyStringstr =(str1.  Length()>str2.length())? STR1:STR2;StringKey =(str= =str1)? str2:str1; For(int i = 0;  I < key. length (); i++) {for(int J = 0,k = key. ) Length()-I.; K!=key.length()+1;j++,k++)//j is the head pointer of key, K is the tail pointer of key {//set a temporary variable to store the maximum same substringStringtemp = key.substring(J,K); If(str.  IndexOf(temp)! =-1) return temp;        }} return ""; /* Wrong idea, unresolved issue//key position in str int index = Str.indexof(key); /2 Using method int indexOf(String str)To determine if key is included in Str when key is not empty and the key is not found in Str, the loop is executed.        When key is empty, it ends the loop.        When key is not empty, inex!=-1 indicates that the maximum same substring is found and the loop is stopped. /While(key.  Length()! = 0 && index ==-1) {index = Str.indexof(key); } The length of the//key is reduced by one, resulting in multiple key public static of the same lengthStringGetkeysubstring(String key){for(int i = 0;  I < key. length (); i++) {for(int J = 0;  J < key. length ()-I && J! = Key.length();)            } }        */    }}

Dark Horse Programmer _ Diary 25_java the maximum identical substring of two strings

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.