Blue Bridge Cup 2017- provincial tournament -C / C ++ - A set of 6 questions

topic

Title: Public maximum substring

The maximum length of common substring question is:
can match the maximum length of all substring find two strings is.

For example: "abcdkkk" and "baabcdadabc",
the longest common substring could be found that the "abcd", so the maximum run length of common sub-4.

The following program is solved using matrix method, which for small-scale strings situation is quite effective solution.

Please analyze the ideas of the solution, and fill the missing part of the whole dash code.


#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: submit only the lack of code, do not submit the existing codes and symbols. Do not submit descriptive text.

Code

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define N 256
 5 int f(const char* s1, const char* s2)
 6 {
 7     int a[N][N];
 8     int len1 = strlen(s1);
 9     int len2 = strlen(s2);
10     int i,j;
11     
12     memset(a,0,sizeof(int)*N*N);
13     int max = 0;
14     for(i=1; i<=len1; i++){
15         for(j=1; j<=len2; j++){
16             if(s1[i-1]==s2[j-1]) {
17                 a[i][j] =a[i-1][j-1]+1;  //填空
18                 if(a[i][j] > max) max = a[i][j];
19             }
20         }
21     }
22     
23     return max;
24 }
25 
26 int main()
27 {
28     printf("%d\n", f("abcdkkk", "baabcdadabc"));
29     return 0;
30 }

 

Guess you like

Origin www.cnblogs.com/memocean/p/12283893.html