暴力枚举或者动态规划 数据范围小两者都可
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 110;
int f[N][N]; // f[i][j] 表示 s1 的前 i 个字符 和 s2 的前 j 个字符的最长公共连续子串长度
string s1, s2;
char s[N], t[N];
int main() {
cin >> s1 >> s2;
int n = s1.length(), m = s2.length();
for (int i = 1; i <= n; i++) s[i] = s1[i - 1];
for (int j = 1; j <= m; j++) t[j] = s2[j - 1];
int ansLength = 0;
int endPos = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i] == t[j]) {
f[i][j] = f[i - 1][j - 1] + 1;
if (f[i][j] >= ansLength) {
ansLength = f[i][j];
endPos = i;
}
} else {
f[i][j] = 0;
}
}
}
cout << ansLength << endl;
if (ansLength > 0) {
cout << s1.substr(endPos - ansLength, ansLength) << endl;
}
return 0;
}