首页 > 其他 > 详细

Common Subsequence

时间:2014-12-17 21:02:08      阅读:192      评论:0      收藏:0      [点我收藏+]
 Common Subsequence
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, x ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc         abfcab
programming    contest 
abcd           mnp

Sample Output

4
2
0
之前实在不会。。问了同学,看了别人的博客。。知道这是动态规划的题。。
实际现在不知道为什么是这样的思想。。但是的确能够解决问题……
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[1000];
char b[1000];
int m[1000][1000];
int main()
{
	int lena,lenb,i,j; 
	while(scanf("%s%s",&a,&b)!=EOF)
	{
		memset(m,0,sizeof(m));
	    lena=strlen(a);
		lenb=strlen(b);
		for(i=1;i<=lena;i++)
		{
			for(j=1;j<=lenb;j++)
			{
                if(a[i-1]==b[j-1])
					m[i][j]=m[i-1][j-1]+1;
				else
				{
					if(m[i-1][j]>m[i][j-1])
						m[i][j]=m[i-1][j];
					else
						m[i][j]=m[i][j-1];
				}
			}
		}
		cout<<m[lena][lenb]<<endl;
	}
	return 0;
}

以下是一个大神的代码。。。找不到链接了,,sorry
bubuko.com,布布扣
#include<stdio.h>
#include<string.h>
char a[500],b[500];
char num[501][501]; ///记录中间结果的数组
char flag[501][501];    ///标记数组,用于标识下标的走向,构造出公共子序列
void LCS(); ///动态规划求解
void getLCS();    ///采用倒推方式求最长公共子序列

int main()
{
    int i;
    strcpy(a,"ABCBDAB");
    strcpy(b,"BDCABA");
    memset(num,0,sizeof(num));
    memset(flag,0,sizeof(flag));
    LCS();
    printf("%d\n",num[strlen(a)][strlen(b)]);
    getLCS();
    return 0;
}

void LCS()
{
    int i,j;
    for(i=1;i<=strlen(a);i++)
    {
        for(j=1;j<=strlen(b);j++)
        {
            if(a[i-1]==b[j-1])   ///注意这里的下标是i-1与j-1
            {
                num[i][j]=num[i-1][j-1]+1;
                flag[i][j]=1;  ///斜向下标记
            }
            else if(num[i][j-1]>num[i-1][j])
            {
                num[i][j]=num[i][j-1];
                flag[i][j]=2;  ///向右标记
            }
            else
            {
                num[i][j]=num[i-1][j];
                flag[i][j]=3;  ///向下标记
            }
        }
    }
}

void getLCS()
{

    char res[500];
    int i=strlen(a);
    int j=strlen(b);
    int k=0;    ///用于保存结果的数组标志位
    while(i>0 && j>0)
    {
        if(flag[i][j]==1)   ///如果是斜向下标记
        {
            res[k]=a[i-1];
            k++;
            i--;
            j--;
        }
        else if(flag[i][j]==2)  ///如果是斜向右标记
            j--;
        else if(flag[i][j]==3)  ///如果是斜向下标记
            i--;
    }

    for(i=k-1;i>=0;i--)
        printf("%c",res[i]);
}


Common Subsequence

原文:http://blog.csdn.net/zuguodexiaoguoabc/article/details/41985231

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!