Longest Common substring

Source: Internet
Author: User

Note the difference between topic understanding and longest common subsequence. The longest common substring solution is dynamic planning, but it is difficult to think of the table construction method.

Note that if the given string is set to str1 and str2, the length of the two is len1 and len2 respectively, then the solution space is as much as len1 * len2? (Assuming that the longest common substring is substr_common, there is only one choice for the end position or start position of substr_common in str1, and len2 for str2, therefore, the maximum solution space is len1 * len2 ). Each solution in the solution space corresponds to its own length, because once the end position is determined, it means that the public substring is found.

If the brute force method is used, it takes O (len1 * len2) to traverse the solution space, and then determine that the length of each solution is O (min (len1, len2), so it is basically an O (n3) algorithm, obviously not appropriate.

 

As mentioned above, this question can be done through dynamic planning. The most important thing about dynamic planning is to find the optimal sub-structure. Generally, the optimal sub-structure means that the problem can be found recursively, the solution for narrowing down the scale of the problem is different from the Division and control method. The small problems of Dynamic Planning overlap, but this problem does not seem so easy to find a recursive expression.

To narrow down the problem scale, the simplest idea is to narrow down the str1 scale, or narrow down the str2 scale at the same time. The narrowing method must be to increase or decrease elements, with dynamic planning, there must be a bottom-up process. There are few two points directly. The two points are the method of divide and conquer. Therefore, the increase and decrease are generally at the beginning and end of the increment and decrement. In this example, to maintain consistency, two strings are set to be reduced from the tail to the header during the analysis of the question, while the header is increased to the tail when the problem is solved. We can construct a table count [len1] [len2], where count [I] [j] indicates that if the public substring ends at the I position of str1, length of the common substring at the end of position j of str2. Starting from the bottom up, this table is added or decreased. Assume that count [I] [j] needs to be calculated. There are several situations:

1. str1 [I] = str2 [j]: count [I] [j] = count [I-1] [J-1] + 1

2. str1 [I]! = Str2 [j]: Obviously, the count [I] [j] = 0 for the solution with I and j as the end point.

So once you know count [I-1] [0 ,...., Len2], count [I] [0 ,...., Len2] can be obtained sequentially. Taking str1 = "1234" and str2 = "2345" as an example, the table construction method is as follows:

As long as you remember the length and end position of the longest substring during table building, you can easily print the longest common substring. The code corresponding to this is as follows:

     
      
       
        
         
          
           
            
             
              
               
                
                 
                  
                   
                    
                     
                      
                       
                        
                         
                          
                           
                            
                             
                              
                               
                                
                                
                                 
                                 
                                 
                                 
                                  
                                   
                                    
                                     
                                      
                                       
                                        
                                         
                                          
                                           
                                            
                                             
                                              
                                               
                                                
                                                 
                                                  
                                                   
                                                    
                                                     
                                                      
                                                       
                                                        
                                                         
                                                          
                                                           
                                                            
                                                             
                                                              
                                                               
                                                                
                                                                 
                                                                  
                                                                   
                                                                    
                                                                     
                                                                      

Related Article

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.