解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <string.h>
int findMaxSpan(char *S, char *S1, char *S2) {
int lenS = strlen(S);
int lenS1 = strlen(S1);
int lenS2 = strlen(S2);
int maxSpan = -1;
// 遍历S中的每一个可能的S1
for (int i = 0; i < lenS; i++) {
// 找到S1在S中的位置
if (strncmp(S + i, S1, lenS1) == 0) {
// 在S1之后的子串中查找S2
char *posS2 = strstr(S + i + lenS1, S2);
while (posS2 != NULL) {
// 计算跨距
int span = posS2 - (S + i + lenS1);
if (span > maxSpan) {
maxSpan = span;
}
// 继续查找下一个S2
posS2 = strstr(posS2 + 1, S2);
}
}
}
return maxSpan;
}
int main() {
char S[301], S1[11], S2[11];
// 从标准输入读取数据
scanf("%[^,],%[^,],%s", S, S1, S2);
// 调用函数计算最大跨距
int result = findMaxSpan(S, S1, S2);
// 输出结果
printf("%d\n", result);
return 0;
}
0.0分
2 人评分