段错误报错解决方法
#include <iostream>
#include <cstring>
using namespace std;
int ch[5001][5001];//需定义成全局变量,否则会栈溢出
int main(){
string str1;
string str2;
cin >> str1 >> str2;
int len1 = str1.size();
int len2 = str2.size();
// int ch[len2+1][len1+1];
//若是在主函数定义则会有段错误报错
for(int i = 0;i <= len2;i++)
for(int j = 0;j <= len1;j++)
ch[i][j] = 0;
int max = 0;
for(int i = 1;i <= len2;i++){
for(int j = 1;j <= len1;j++){
if(str2[i-1]==str1[j-1]){
ch[i][j] = ch[i-1][j-1]+1;
if(ch[i][j] > max) max = ch[i][j];
}
}
}
cout << max << endl;
return 0;
}