最长公共子序列(hdu1159)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxlen = 1000;
char x[maxlen];
char z[maxlen];
int F[maxlen][maxlen];

int DP() {
    int xlen = strlen(x);
    int zlen = strlen(z);
    for (int i = 0; i < xlen; i++) {
        if (x[i] == z[0]) {
            F[i][0] = 1;
            continue;
        }
        if (i == 0) {
            F[i][0] = 0;
            continue;
        }
        F[i][0] = F[i-1][0];
    }
    for (int i = 0; i < zlen; i++) {
        if (z[i] == x[0]) {
            F[0][i] = 1;
            continue;
        }
        if (i == 0) {
            F[0][i] = 0;
            continue;
        }
        F[0][i] = F[0][i-1];
    }
    for (int i = 1; i < xlen; i++) {
        for (int j = 1; j < zlen; j++) {
            if (x[i] == z[j]) {
                F[i][j] = F[i-1][j-1] + 1;
                continue;
            }
            F[i][j] = max(F[i-1][j], F[i][j-1]);
        }
    }
    printf("%d\n", F[xlen-1][zlen-1]);
    return 0;
}

int init() {
    memset(F, 0, sizeof(F));
    return 0;
}

int main()
{
    init();
    while (2 == scanf("%s%s", x, z)) {
        DP();
        init();
    }
    return 0;
}

你可能感兴趣的:(最长公共子序列(hdu1159))