动态规划-最长公共子串

    #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;  
    }   

 

posted @ 2018-04-09 09:49  思丿无邪  阅读(98)  评论(0编辑  收藏  举报