现在的位置: 首页 > 综合 > 正文

求最长公共子序列(不连续)求相似度

2018年02月22日 ⁄ 综合 ⁄ 共 495字 ⁄ 字号 评论关闭

转移方程:if(s1[i]==s2[j])c[i][j]=c[i-1][j-1]+1; else c[i][j]=max(c[i-1][j],c[i][j-1]);

#include<stdio.h>
#include<string.h>
int main()
{
    int c[100][100];
    char str1[100],str2[100];
    while(scanf("%s %s",str1,str2)>0)
    {
        int len1=strlen(str1);
        int len2=strlen(str2);
        for(int i=0;i<len1;i++) c[i][0]=0;
        for(int i=0;i<len2;i++) c[0][i]=0;
        for(int i=1;i<=len1;i++)
        for(int j=1;j<=len2;j++)
        {
            if(str1[i-1]==str2[j-1]) c[i][j]=c[i-1][j-1]+1;
            else if(c[i-1][j]>c[i][j-1]) c[i][j]=c[i-1][j];
            else c[i][j]=c[i][j-1];
        }
        printf("%d\n",c[len1][len2]);
    }
}

抱歉!评论已关闭.