请写出最长公共子串的c++代码
时间: 2023-12-08 20:06:10 浏览: 104
Sure, here is the C code for finding the longest common substring:
```
#include <stdio.h>
#include <string.h>
void findLCS(char* str1, char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int LCSuff[len1 + 1][len2 + 1];
int result = 0;
for (int i = 0; i <= len1; i++) {
for (int j = 0; j <= len2; j++) {
if (i == 0 || j == 0)
LCSuff[i][j] = 0;
else if (str1[i - 1] == str2[j - 1]) {
LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;
result = LCSuff[i][j] > result ? LCSuff[i][j] : result;
}
else
LCSuff[i][j] = 0;
}
}
printf("The length of the longest common substring is %d", result);
}
int main() {
char str1[] = "abcdxyz";
char str2[] = "xyzabcd";
findLCS(str1, str2);
return 0;
}
```
Hope this helps!
阅读全文