Blue Bridge Cup Daily One Question 1.8 2017 Provincial Competition Group A 6. The largest common substring [DP]

Title description

  2017 Blue Bridge Cup Software Provincial Competition C++ University Group A, Question 6 "The Largest Common Substring ".
  A code fill-in-the-blank question, 80% is also a sub-question.
  Because it is not difficult, Teacher Luo will do it again.


The problem of maximum common substring length is: what
is the maximum length that can be matched among all substrings of two strings.

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

The following program is solved by the matrix method, which is a relatively effective solution for the case where the string size is not large.

Please analyze the idea of ​​the solution and fill in the missing code in the underlined part.

#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"));
	return 0;
}

Note: Only submit missing codes, do not submit existing codes and symbols. Don’t submit explanatory text.

https://blog.csdn.net/weixin_43914593/article/details/112336419

With this problem, TAT, which will be done as well, I have talked about it in the algorithm class, it is very simple

	a[i][j] = a[i-1][j-1] + 1;

 

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/112983854