杭電1159—Common Subsequence題解

求兩個字符串的最長公共子序列

#include<cstdio>
#include<cstring>
int maximum_length_common_subsequence(char *op,char *jk);
int max(int a,int b);
int main()
{
    char op[505],jk[505];
    while(scanf("%s%s",op,jk)!=EOF)
    {
        int a=maximum_length_common_subsequence(op,jk);
        printf("%d\n",a);
    }
    return 0;
}
int maximum_length_common_subsequence(char *op,char *jk)
{
    int i=strlen(op),j=strlen(jk),x,y;
    int nm[505][505];
    memset(nm,0,sizeof(nm));
    for(x=1;x<=i;x++)
        for(y=1;y<=j;y++)
    {
        if(op[x-1]==jk[y-1])
            nm[x][y]=nm[x-1][y-1]+1;
        else
            nm[x][y]=max(nm[x-1][y],nm[x][y-1]);
    }
    return nm[i][j];
}
int max(int a,int b)
{
    return a>b?a:b;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章