最长公共子序列问题·

来源:互联网 发布:淘宝乐高军事玩具大全 编辑:程序博客网 时间:2024/06/10 16:31
最长公共子序列问题
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

给定两个序列X=
Input

输入数据有多组,每组有两行 ,每行为一个长度不超过500的字符串(输入全是大写英文字母(A,Z)),表示序列X和Y。
Output

每组输出一行,表示所求得的最长公共子序列的长度,若不存在公共子序列,则输出0。
Example Input

ABCBDAB
BDCABA
Example Output

4

#include <stdio.h>#include<string.h>int max (int a ,int b){    if(a>b) return a;    else return b;}int main (){    char s[650],v[650];    int dp[650][650];    int n,m,i,j;    while (~scanf("%s %s",s,v))    {        n=strlen(s);        m=strlen(v);        memset(dp,0,sizeof(dp));        for(i=1;i<=n;i++)//dp是从dp[1]开始但字符串数组是从0开始        {            for(j=1;j<=m;j++)            {                if(s[i-1]==v[j-1])                {                    dp[i][j]=dp[i-1][j-1]+1;                }                else                {                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);                }            }        }        printf("%d\n",dp[n][m]);    }}



0 0
原创粉丝点击