UVa 10192 - Vacation

传送门

UVa 10192 - Vacation

题意

最长公共子序列。我好像有点明白LRJ说第一版习题质量不高的原因了。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100 + 10;

int dp[MAXN][MAXN];

int main()
{
    //freopen("input.txt", "r", stdin);
    char a[MAXN], b[MAXN];
    int i, j, alen, blen, cases = 1;
    while (gets(a), a[0] != '#')
    {
        memset(dp, 0, sizeof(dp));
        gets(b);
        alen = strlen(a);
        blen = strlen(b);
        for (i = 1; i <= alen; i++)
            for (j = 1; j <= blen; j++)
            {
                if (a[i - 1] == b[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("Case #%d: you can visit at most %d cities.\n", cases++, dp[alen][blen]);
    }
    return 0;
}

Powered by Jekyll and Theme by solid