Algorithm - maximum common substring

Posted by Huuggee on Sun, 06 Mar 2022 08:17:07 +0100

Title Description

This topic is code completion. Please complete the source code given in the topic and copy it to the code box on the right. Select the corresponding compilation language (C/Java) and submit it. If the source code language given in the title is not unique, you only need to select one to complete and submit. After copying, delete the underline in the blank part of the source code and fill in your answer. If it fails to pass after submission, in addition to considering the errors in filling in the blanks, it is also necessary to pay attention to whether there are errors due to changes in the non filled blanks after copying.

The problem of maximum common substring length is to find the maximum length that can be matched in all substrings of two strings.

For example, "abcdkkk" and "baabcddabc", the longest common substring that can be found is "abcd", so the maximum common substring length is 4.

The following program is solved by matrix method, which is a more effective solution for the case of small string size.

Please analyze the idea of this solution and complete the missing code in the underlined part.

source code

C

#include <stdio.h>
#include <string.h>

#define N 256
int f(const char* s1, const char* s2)
{
    int a[N][N];
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int i,j;
    
    memset(a,0,sizeof(int)*N*N);
    int max = 0;
    for(i=1; i<=len1; i++){
        for(j=1; j<=len2; j++){
            if(s1[i-1]==s2[j-1]) {
                a[i][j] = __________________________; 
                if(a[i][j] > max) max = a[i][j];
            }
        }
    }
    
    return max;
}

int main()
{
    printf("%d\n", f("abcdkkk", "baabcdadabc"));
    printf("%d\n", f("aaakkkabababa", "baabababcdadabc"));
    printf("%d\n", f("abccbaacbcca", "ccccbbbbbaaaa"));    
    printf("%d\n", f("abcd", "xyz"));
    printf("%d\n", f("ab", "ab"));
    return 0;
}

Java

import java.util.Scanner;
public class Main
{
    static int f(String s1, String s2)
    {
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        
        int[][] a = new int[c1.length+1][c2.length+1];
        
        int max = 0;
        for(int i=1; i<a.length; i++){
            for(int j=1; j<a[i].length; j++){
                if(c1[i-1]==c2[j-1]) {
                    a[i][j] = __________________________; 
                    if(a[i][j] > max) max = a[i][j];
                }
            }
        }
        
        return max;
    }
    
    public static void main(String[] args){
        int n = f("abcdkkk", "baabcdadabc");
        System.out.println(n);
        System.out.println(f("aaakkkabababa", "baabababcdadabc"));
        System.out.println(f("abccbaacbcca", "ccccbbbbbaaaa"));
        System.out.println(f("abcd", "xyz"));
        System.out.println(f("ab", "ab"));
        
    }
}

Operational limits

  • Maximum running time: 1s
  • Maximum operating memory: 256M

Problem solving ideas:

This is a code filling question. Just fill in the blank code. The question constructs a two-dimensional array according to the length of the two input strings. Each empty space of the two-dimensional array is used to store the maximum matching length of the character corresponding to the current row and column value. For example, when the first string is the example "abcdkkk" given by the title and the second string is "baabcdadabc", a matrix of eight rows and twelve columns will be created. When traversing the first character a of the first string, the second string starts from b. first, judge whether a is equal to b and the result is false, so no operation is required, Directly traverse the second character a of the second string to judge whether a is equal to a, and the result is true, so the value at this position of the two-dimensional array is equal to the value of a[i - 1][j - 1] plus 1. If this is repeated, the final max is the longest matching length. The Java code implementation of the algorithm is as follows:

import java.util.Scanner;
public class Main
{
    static int f(String s1, String s2)
    {
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        
        int[][] a = new int[c1.length+1][c2.length+1];
        
        int max = 0;
        for(int i=1; i<a.length; i++){
            for(int j=1; j<a[i].length; j++){
                if(c1[i-1]==c2[j-1]) {
                    a[i][j] = a[i - 1][j - 1] + 1; 
                    if(a[i][j] > max) max = a[i][j];
                }
            }
        }
        
        return max;
    }
    
    public static void main(String[] args){
        int n = f("abcdkkk", "baabcdadabc");
        System.out.println(n);
        System.out.println(f("aaakkkabababa", "baabababcdadabc"));
        System.out.println(f("abccbaacbcca", "ccccbbbbbaaaa"));
        System.out.println(f("abcd", "xyz"));
        System.out.println(f("ab", "ab"));
        
    }
}

Topics: Java Algorithm