码迷,mamicode.com
首页 > 其他好文 > 详细

【求最大公共子串长度】

时间:2017-04-12 09:52:35      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:bcd   fine   初始化   公共子串   ems   pre   str   ++   style   

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

#define N 256
int fun(const char* s1, const char* s2)
{
    int a[N][N];
    int len1 = strlen(s1);    //字符串s1的长度 
    int len2 = strlen(s2);    //字符串s2的长度 
    int i,j;
    
    memset(a,0,sizeof(int)*N*N);    //初始化数组为0 
    
    int max = 0;
    for(i=1; i<=len1; i++){
        for(j=1; j<=len2; j++){
            if(s1[i-1]==s2[j-1]) {
                a[i][j] = a[i-1][j-1] + 1;  
                if(a[i][j] > max) {
                    max = a[i][j];
                } 
            }
        }
    }
    
    return max;
}

int main()
{
    printf("%d\n", fun("abcdkkk", "baabcdadabc"));    //4
    return 0;
}

 

【求最大公共子串长度】

标签:bcd   fine   初始化   公共子串   ems   pre   str   ++   style   

原文地址:http://www.cnblogs.com/libra-yong/p/6683201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!