题解 | #公共子串计算#

公共子串计算

https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

#include <stdio.h>
#include <string.h>

int main() {
    char s1[150], s2[150];
    int maxl = 0;
    while (scanf("%s\n%s", s1, s2) != EOF) {
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (s1[i] == s2[j]) {
                    int tempmax = 0;
                    int k = 0;
                        while ((i + k < len1 )&&(j + k < len2)&&(s1[i + k] == s2[j + k])) {
                            k++;
                            tempmax++;
                        }

                    if (tempmax > maxl) maxl = tempmax;
                }
            }
        }
        printf("%d\n", maxl);
    }
    return 0;
}

全部评论

相关推荐

12-27 22:49
门头沟学院 Java
点赞 评论 收藏
分享
12-04 15:36
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务