题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
#include <iostream> #include <vector> using namespace std; // 套公式做的 int main() { string str1, str2; while (cin >> str1 >> str2) { // 注意 while 处理多个 case int len1 = str1.length(); int len2 = str2.length(); int max = 0; vector<vector<int>> matrix(len1+1, vector<int>(len2+2, 0)); for(int i = 1; i < len1+1; i++){ for(int j = 1; j < len2+1; j++){ if(str1[i-1] == str2[j-1]){ matrix[i][j] = matrix[i-1][j-1] + 1; }else{ matrix[i][j] = 0; } if(matrix[i][j] > max){ max = matrix[i][j]; } } } cout << max << endl; } } // 64 位输出请用 printf("%lld")