求两个字符串的公共子串的最大长度

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 1100
 6 
 7 char s[N];
 8 char t[N];
 9 
10 int dp[N][N];
11 
12 int main()
13 {
14     cin >> s;
15     cin >> t;
16     int lens = strlen(s);
17     int lent = strlen(t);
18     int mx = 0;
19     for (int i = 0; i < lens; i++) {
20         for (int j = 0; j < lent; j++) {
21             if (s[i] == t[j]) {
22                 if (i == 0 || j == 0) {
23                     dp[i][j] = 1;
24                 } else {
25                     dp[i][j] = dp[i - 1][j - 1] + 1;
26                 }
27 
28                 mx = max(mx, dp[i][j]);
29             }
30         }
31     }
32 
33     cout << mx << endl;
34     return 0;
35 }
36 /*
37 abcde
38 bcd
39 */
原文地址:https://www.cnblogs.com/lezhifang/p/7482821.html