假设有两个字符串(可能包含空格),找出其中最长的公共连续子串,并输出其长度。
输入描述:
输入为两行字符串(可能包含空格),长度均小于等于50
输出描述:
输出为一个整数,表示最长公共连续子串的长度
输入例子:
abcde
abgde
输出例子:
2
ab
de
假设有两个字符串(可能包含空格),找出其中最长的公共连续子串,并输出其长度。
输入描述:
输入为两行字符串(可能包含空格),长度均小于等于50
输出描述:
输出为一个整数,表示最长公共连续子串的长度
输入例子:
abcde
abgde
输出例子:
2
ab
de
你题目的解答代码如下:
#include <stdio.h>
#include <string.h>
#define N 50
int main(){
char s1[N],s2[N];
int dp[N][N],i,j,max_len=0;
gets(s1);
gets(s2);
for(i=0;i<strlen(s1);i++){
for(j=0;j<strlen(s2);j++){
if(s1[i]==s2[j]){
if(i>0&&j>0){
dp[i][j]=dp[i-1][j-1]+1;
}else{
dp[i][j]=1;
}
if(max_len<dp[i][j]){
max_len=dp[i][j];
}
}
}
}
printf("%d\n",max_len);
return 0;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!