POJ 1458 Common Subsequence

求最长公共子序列,题目居然没有给出字符串最大长度。

 

/*Accepted 428K 0MS C++ 570B 2012-04-11 10:26:03 */



#include<cstdlib>

#include<cstring>

#include<cstdio>

#define MAXN 1 << 8

#define max(a, b) ( a > b ? a : b)

char X[MAXN], Y[MAXN];

int f[MAXN][MAXN];

int x, y;



void dp()

{

    memset( f, 0, sizeof f);

    x = strlen(X + 1), y = strlen( Y + 1);

    for( int i = 1; i <= x; i ++)

        for( int j = 1; j <= y; j ++)

        {

            if( X[i] == Y[j])

                f[i][j] = f[i - 1][j - 1] + 1;

            else

                f[i][j] = max( f[i - 1][j], f[i][j - 1]);

        }

}



int main()

{

    while( scanf( "%s%s", X + 1, Y + 1) == 2)

    {

        dp();

        printf( "%d\n", f[x][y]);

    }

    return 0;

}

 

 

 

你可能感兴趣的:(sequence)